Arguments normalization test

Benchmark created on


Setup

function vargs(fn) {
        var slice = Array.prototype.slice,
            paramsCount = fn.length
    
        return function () {
            var argsCount = arguments.length,
                lastArg = arguments[argsCount - 1],
                args
            if (argsCount < paramsCount && typeof lastArg === 'function') {
                args = slice.call(arguments, 0, argsCount - 1)
                for (var i = paramsCount - argsCount; i; i -= 1)
                    args.push(undefined)
                args.push(lastArg)
                return fn.apply(this, args)
            }
            return fn.apply(this, arguments)
        }
    }
    
    var add1 = vargs(function (a, b, options, callback) {
        options = options || {}
        var sum = a + b
        return callback(null, sum)
    })
    
    var add2 = function (a, b, options, callback) {
        if (typeof options === 'function') {
            callback = options
            options = {}
        }
        var sum = a + b
        return callback(null, sum)
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Arguments normalization decorator
var total1

add1(1, 2, function (error, sum) {
    total1 = sum
})
ready
Inline arguments normalization
var total2

add2(1, 2, function (error, sum) {
    total2 = sum
})
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.