Skip to content

Instantly share code, notes, and snippets.

@uupaa
Last active December 21, 2015 08:19
Show Gist options
  • Select an option

  • Save uupaa/6277337 to your computer and use it in GitHub Desktop.

Select an option

Save uupaa/6277337 to your computer and use it in GitHub Desktop.
ArrayLike/Array shallow copy bench mark

bench result

  • Chrome 30 で A = B*3
  • Firefox 22 で A = B
function A(obj, i) {
    var rv = Array.apply(null, obj);
    rv.push(i);
}

function B(obj, i) {
    var rv = Array.prototype.slice.call(obj);
    rv.push(i);
}


function test1() {
    var now = Date.now();
    var array1 = [];
    for (var i = 0; i < 10000; ++i) {
        array1.push(i);
        A(array1);
    }
    console.log("tick1: " + (Date.now() - now));
}


function test2() {
    var now = Date.now();
    var array2 = [];
    for (var i = 0; i < 10000; ++i) {
        array2.push(i);
        B(array2);
    }
    console.log("tick2: " + (Date.now() - now));
}

Date.now() % 2 ? (test1(), test2()) : (test2(), test1());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment