array extras second argument

Benchmark created by WebReflection on


Description

It's a common pattern to use forEach, mp, filter, and others creating each time a closure runtime. This test aim is to compare different approaches to solve the same thing, bringing variables via closure or using the second argument

Setup

var // 10 items is enough
        // since this test is not about the length
        items = [0,1,2,3,4,5,6,7,8,9],
        result
    ;
    
    function pow(i) {
        return i * i;
    }
    
    function copy(value, i, items) {
        this[i] = items[i];
    }
    
    function redundantCopy(value, i, items) {
        this.s[this.j++] = items[i];
    }

Test runner

Ready to run.

Testing in
TestOps/sec
common map
result = items.map(function (value) {
    return value * value;
});
ready
external map
result = items.map(pow);
ready
common copy pattern
var that = [];
items.forEach(function (value, i, items) {
    that[i] = items[i];
});
ready
external copy pattern
var that = [];
items.forEach(copy, that);
ready
common multiple references
var that = [], j = 0;
items.forEach(function (value, i, items) {
    that[j++] = items[i];
});
ready
external multiple references
var that = [];
items.forEach(redundantCopy, {s: that, j: 0});
ready

Revisions

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

  • Revision 1: published by WebReflection on