Instantiating Arrays

JavaScript performance comparison

Test case created by Joshua Harrison

Info

Tests the cost of instantiating and copying classic arrays vs. typed arrays. Starts with an array of length 2. Create a second array of double the length and copy the first array's data into the second array.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var classicArray = [2];
    classicArray[0] = 1;
    classicArray[1] = 2;
    var typedArray = new Uint8Array(2);
    typedArray[0] = 1;
    typedArray[1] = 2;
    var iterations = 15;
    var current = 0;
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Instantiate and copy classic arrays.
while (current < iterations) {
    newArray = new Array(classicArray.length * 2);
    for (var i = 0; i < 2; i++) {
        for (var j = 0; j < classicArray.length; j++) {
            newArray[(i * classicArray.length) + j] = classicArray[j];
        }
    }
    classicArray = newArray;
    current++;
}
pending…
Instantiate and copy typed arrays.
while (current < iterations) {
  var next = new Uint8Array(typedArray.length * 2);
  next.set(typedArray);
  next.set(typedArray, typedArray.length);
  typedArray = next;
  current++;
}
pending…

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

Compare results of other browsers

0 comments

Add a comment