opterion
8/23/2017 - 12:18 PM

V8 performance optimization utils

V8 performance optimization utils

// doesn't leak arguments
function extractArgs() {
    //.length is just an integer, this doesn't leak
    //the arguments object itself
    var args = new Array(arguments.length);
    for(var i = 0; i < args.length; ++i) {
        //i is always valid index in the arguments object
        args[i] = arguments[i];
    }
    return args;
}

/*
However, if you have a build-step, this can also be achieved with a macro that doesn't necessitate
the use of source maps and lets the source code stay valid JavaScript:
*/
function doesntLeakArguments() {
    INLINE_SLICE(args, arguments);
    return args;
}
/*
The above technique is used in bluebird and the result is expanded into this in the build step:
*/
function doesntLeakArguments() {
    var $_len = arguments.length;
    var args = new Array($_len); 
    for(var $_i = 0; $_i < $_len; ++$_i) {
        args[$_i] = arguments[$_i];
    }
    return args;
}

/*
What is safe arguments usage? - https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#what-is-safe-arguments-usage
*/