const size = 1024;
var buffer64 = new ArrayBuffer(size*8);
var buffer32 = new ArrayBuffer(size*4);
var f64View = new Float32Array(buffer64, 0, size);
var f32View = new Float32Array(buffer32, 0, size);
var numericArray = [];
for (let i=0; i<size; i++) {
const someVal = Math.random();
numericArray[i] = someVal;
f64View[i] = someVal;
f32View[i] = someVal;
}
const resultArray = numericArray.map(s => {
return (s == 1);
});
const resultArray = f64View.map(s => {
return (s == 1);
});
const resultArray = f32View.map(s => {
return (s == 1);
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
numeric array | |
Float64Array | |
Float32Array |
Test name | Executions per second |
---|---|
numeric array | 183934.2 Ops/sec |
Float64Array | 181282.2 Ops/sec |
Float32Array | 188244.4 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Definition:
The benchmark tests the performance difference between three approaches:
map()
method on an array of random true/false values (numericArray
).map()
method on a typed array with 8-byte elements ( Float64 type), where each element represents either 0 or 1.map()
method on a typed array with 4-byte elements (Float32 type).What is tested:
The benchmark measures the execution speed of the map()
method for each approach, comparing the number of executions per second.
Options compared:
Pros and Cons:
Library and Purpose:
The map()
method is a built-in JavaScript function that applies a specified transformation to each element of an array. In this benchmark, the map()
method is used to create a new array with the same number of elements as the original array, but with transformed values (either true or false).
Special JS feature:
There are no special JavaScript features mentioned in the benchmark definition.
Other alternatives:
To further compare the performance of these approaches, other variations could be tested:
forEach()
instead of map()
.ArrayBuffer
with a Uint8Array view.Keep in mind that the specific options and alternatives will depend on the goals of the benchmark.