<span id="foo"></span>
const span = document.getElementById ("foo");
class Float32ArrayExtended extends Float32Array {
constructor (val) {
super(1);
this[0] = val;
}
get x () { return this[0]; }
set x (v) { this[0] = v; }
}
class Int32ArrayExtended extends Int32Array {
constructor (val) {
super(1);
this[0] = val;
}
get x () { return this[0]; }
set x (v) { this[0] = v; }
}
const size = 10000;
const array = new Array(16).fill(1);
const arrayEF32 = new Float32ArrayExtended (16);
const arrayF32 = new Float32Array (16);
const arrayEI32 = new Int32ArrayExtended (16);
const arrayI32 = new Int32Array (16);
let x = 1;
const obj = {
get x() {
return array[0];
},
set x(value) {
array[0] = value;
}
};
let sum = 0;
for (let i = 0; i < size; i++) {
array[0] = Math.random () * 1000;
sum += array[0];
}
foo.innerText = sum;
let sum = 0;
for (let i = 0; i < size; i++) {
obj.x = Math.random () * 1000;
sum += obj.x;
}
foo.innerText = sum;
let sum = 0;
for (let i = 0; i < size; i++) {
arrayF32[0] = Math.random () * 1000;
sum += arrayF32[0];
}
foo.innerText = sum;
let sum = 0;
for (let i = 0; i < size; i++) {
arrayEF32.x = Math.random () * 1000;
sum += arrayEF32.x;
}
foo.innerText = sum;
let sum = 0;
for (let i = 0; i < size; i++) {
arrayI32[0] = Math.random () * 1000;
sum += arrayI32[0];
}
foo.innerText = sum;
let sum = 0;
for (let i = 0; i < size; i++) {
arrayEI32.x = Math.random () * 1000;
sum += arrayEI32.x;
}
foo.innerText = sum;
let sum = 0;
for (let i = 0; i < size; i++) {
x = Math.random () * 1000;
sum += x;
}
foo.innerText = sum;
let sum = 0;
for (let i = 0; i < size; i++) {
array[0] = Math.random () * 1000;
sum += array[0];
}
foo.innerText = sum;
let sum = 0;
for (let i = 0; i < size; i++)
sum += Math.random () * 1000;
foo.innerText = sum;
let sum = 0;
for (let i = 0; i < size; i++) {
const x = Math.random () * 1000;
sum += x;
}
foo.innerText = sum;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array[0] | |
set / get | |
float 32 | |
extended float 32 | |
int 32 | |
extended int 32 | |
simple | |
array[0] 2 | |
simple 2 | |
simple 3 |
Test name | Executions per second |
---|---|
array[0] | 9386.4 Ops/sec |
set / get | 1117.2 Ops/sec |
float 32 | 8900.2 Ops/sec |
extended float 32 | 9090.9 Ops/sec |
int 32 | 8791.2 Ops/sec |
extended int 32 | 8863.5 Ops/sec |
simple | 1145.0 Ops/sec |
array[0] 2 | 9573.9 Ops/sec |
simple 2 | 11528.8 Ops/sec |
simple 3 | 11625.6 Ops/sec |
The provided benchmark tests various approaches to accessing and modifying data in JavaScript, particularly with respect to arrays and typed arrays. It explores how different methods perform in terms of execution speed when repeatedly manipulating the same underlying value. Here's a breakdown of the test cases, their pros and cons, and considerations:
Simple Assignment (simple
, simple 2
, simple 3
):
x
and accumulates the sum.Array Access (array[0]
, array[0] 2
):
Object Getter/Setter (set / get
):
Typed Arrays:
Float32Array (float 32
):
Extended Float32Array (extended float 32
):
Float32Array
with a getter/setter property x
to access the first element.Int32Array (int 32
):
Float32Array
, but specifically for 32-bit signed integers.Float32Array
.Extended Int32Array (extended int 32
):
Int32Array
with getter/setter x
to interact with the first element.simple
cases perform the fastest, demonstrating the overhead of accessing properties and managing arrays or typed arrays. Direct variable manipulation leads to optimized speed.Overall, this benchmark provides valuable insights into the performance characteristics and trade-offs involved in different data manipulation techniques within JavaScript. The right approach largely depends on the specific use case and the requirements for performance versus maintainability.