native vs typed js array speed

JavaScript performance comparison

Revision 55 of this test case created by Ger Hobbelt

Preparation code

<script>
  var arraySize = 10000;
  var nativeArray = new Array(arraySize);
  var int8Array = new Int8Array(arraySize);
  var int16Array = new Int16Array(arraySize);
  var int32Array = new Int32Array(arraySize);
  var float32Array = new Float32Array(arraySize);
  var float64Array = new Float64Array(arraySize);

  var testnr = 0;
  var freq = 10000;

</script>
      
<script>
Benchmark.prototype.setup = function() {
  var sum = 0;
  
  

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Native Write
for (var i = 0; i < arraySize; ++i) {
  nativeArray[i] = i + 0.1;
}
pending…
Int32 Write
for (var i = 0; i < arraySize; ++i) {
  int32Array[i] = i + 0.1;
}
pending…
Float32 Write
for (var i = 0; i < arraySize; ++i) {
  float32Array[i] = i + 0.1;
}
pending…
Native Read
sum = 0;
for (var i = 0; i < arraySize; ++i) {
  sum += nativeArray[i];
}
testnr++;
if (testnr % freq === 0) { 
  console.log("native: ", sum, nativeArray[0], nativeArray[1]);
  sum = 0;
}
pending…
Int32 Read
sum = 0;
for (var i = 0; i < arraySize; ++i) {
  sum += int32Array[i];
}
testnr++;
if (testnr % freq === 0) { 
  console.log("int32: ", sum, int32Array[0], int32Array[1]);
  sum = 0;
}
pending…
Float32 Read
sum = 0;
for (var i = 0; i < arraySize; ++i) {
  sum += float32Array[i];
}
testnr++;
if (testnr % freq === 0) { 
  console.log("float32: ", sum, float32Array[0], float32Array[1]);
  sum = 0;
}
pending…
Int16 Write
for (var i = 0; i < arraySize; ++i) {
  int16Array[i] = i + 0.1;
}
pending…
Int16 Read
sum = 0;
for (var i = 0; i < arraySize; ++i) {
  sum += int16Array[i];
}
testnr++;
if (testnr % freq === 0) { 
  console.log("int16: ", sum, int16Array[0], int16Array[1]);
  sum = 0;
}
pending…
Int8 Write
for (var i = 0; i < arraySize; ++i) {
  int8Array[i] = i + 0.1;
}
pending…
Int8 Read
sum = 0;
for (var i = 0; i < arraySize; ++i) {
  sum += int8Array[i];
}
testnr++;
if (testnr % freq === 0) { 
  console.log("int8: ", sum, int8Array[0], int8Array[1]);
  sum = 0;
}
pending…
Alternative Native Write
nativeArray = new Array(arraySize);

for (var i = 0; i < arraySize; ++i) {
  nativeArray.push(i + 0.1);
}
pending…
Alt Native Read
sum = 0;
for (var i in nativeArray) {
  sum += nativeArray[i];
}
testnr++;
if (testnr % freq === 0) { 
  console.log("alt native: ", sum, nativeArray[0], nativeArray[1]);
  sum = 0;
}
pending…
Float64 Write
for (var i = 0; i < arraySize; ++i) {
  float64Array[i] = i + 0.1;
}
pending…
Float64 Read
sum = 0;
for (var i = 0; i < arraySize; ++i) {
  sum += float64Array[i];
}
testnr++;
if (testnr % freq === 0) { 
  console.log("float64: ", sum, float64Array[0], float64Array[1]);
  sum = 0;
}

pending…

Compare results of other browsers

Revisions

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

0 Comments