const size = 1024;
var buffer = new ArrayBuffer(size);
var u8View = new Uint8Array(buffer, 0, size);
var buffer2 = new ArrayBuffer(size);
var u8BoolView = new Uint8Array(buffer2, 0, size/8);
var booleanArray = [];
for (let i=0; i<size; i++) {
const someVal = Math.random()*1;
//typed array stores (0 or 1)
u8View[i] = someVal;
//boolean array stores (false or true)
booleanArray.push(someVal == 1);
}
for (let i=0; i<size/8; i++) {
let some256Val = Math.random()*255;
for (let j=0; j<8; j++) {
}
let FLAG_A = some256Val &= 128;
let FLAG_B = some256Val &= 64;
let FLAG_C = some256Val &= 32;
let FLAG_D = some256Val &= 16;
let FLAG_E = some256Val &= 8;
let FLAG_F = some256Val &= 4;
let FLAG_G = some256Val &= 2;
let FLAG_H = some256Val &= 1;
u8BoolView[i] = (FLAG_A | FLAG_B | FLAG_C | FLAG_D | FLAG_E | FLAG_F | FLAG_G | FLAG_H);
}
const resultArray = booleanArray.map(s => {
return (s == 1);
});
const resultArray = u8View.map(s => {
return (s == 1);
});
'use strict';
const resultArray = booleanArray.map(s => {
return (s == 1);
});
'use strict';
const resultArray = u8View.map(s => {
return (s == 1);
});
'use strict';
const resultArray = u8BoolView.map(s => {
return ((s &= 128) == 1);
});
const resultArray = u8View.fill(1);
const resultArray = u8BoolView.fill(255);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
boolean array | |
typed Array with uInt8 | |
boolean array - Strict | |
typed Array with uInt8 - Strict | |
weird boolean - Strict | |
uint8 .fill() | |
weird boolean - .fill |
Test name | Executions per second |
---|---|
boolean array | 796062.4 Ops/sec |
typed Array with uInt8 | 2178973.2 Ops/sec |
boolean array - Strict | 717859.2 Ops/sec |
typed Array with uInt8 - Strict | 2161385.0 Ops/sec |
weird boolean - Strict | 12857642.0 Ops/sec |
uint8 .fill() | 52906320.0 Ops/sec |
weird boolean - .fill | 73618520.0 Ops/sec |
Let's break down what is being tested and analyzed.
Benchmark Definition
The benchmark compares the performance of different approaches to creating an array of boolean values, specifically:
BooleanArray
(an array of booleans)Uint8Array
) with a view on a Buffer
objectUint8Array
) with a specific bitmasking approachTest Cases
The individual test cases compare the performance of these approaches under different conditions:
BooleanArray
Uint8Array
) with a view on a Buffer
object'use strict';
)Uint8Array
) with a specific maskuint8 .fill()
and 255 for weird boolean - .fill
)Library
No external libraries are used in this benchmark.
Special JS feature or syntax
The only special JavaScript feature used is the bitmasking approach, which is demonstrated in the "weird boolean" test case. This involves using bitwise operations (&
, |
) to create a mask and apply it to an array of values.
Analysis
The results show that using a typed array (Uint8Array
) with a view on a Buffer
object (test cases 2, 5) is significantly faster than using a BooleanArray
(test case 1). The bitmasking approach ("weird boolean" test cases) also shows good performance.
However, running in strict mode (using 'use strict';
) seems to have a negative impact on performance for all test cases except "weird boolean - .fill".
It's worth noting that these results are specific to the browser and device being tested (Firefox 128 on Windows Desktop).