var memory = new ArrayBuffer(1024 * 1024);
var memoryF32 = new Float32Array(memory);
var memoryU32 = new Uint32Array(memory);
for (let i = 0; i < 1024; ++i) {
memoryF32[i] = Math.random();
}
for (let i = 1; i < 1024; ++i) {
memoryF32[i-1] = memoryF32[i] * 2;
}
for (let i = 1; i < 1024; ++i) {
memoryU32[i + 1024] = memoryF32[i] > 0.5 ? 1 : 0;
}
const readF32 = (idx) => memoryF32[idx];
const writeF32 = (idx, value) => memoryF32[idx] = value;
const writeU32 = (idx, value) => memoryU32[idx+1024] = value;
for (let i = 0; i < 1024; ++i) {
writeF32(i, Math.random());
}
for (let i = 1; i < 1024; ++i) {
writeF32(i-1, readF32(i) * 2);
}
for (let i = 1; i < 1024; ++i) {
writeU32(i, memoryF32[i] > 0.5 ? 1 : 0);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array access | |
Function access |
Test name | Executions per second |
---|---|
Array access | 2106.1 Ops/sec |
Function access | 2147.5 Ops/sec |
Let's break down the benchmark and its options.
Benchmark Overview
The benchmark compares two approaches: function call vs array access. The test case creates an array of 1024 elements, fills it with random values, and then performs operations on it using both methods.
Options Compared
readF32
and writeF32
take an index as an argument, while writeU32
takes an additional value.Pros and Cons of Each Approach
Function Call
Pros:
Cons:
value
)Array Access
Pros:
Cons:
Library Used
None. This benchmark only uses built-in JavaScript functionality, not any external libraries.
Special JS Features or Syntax
This benchmark does not explicitly use any special JavaScript features or syntax beyond standard array and function operations.
Other Considerations
The benchmark's results are influenced by various factors, such as:
memoryF32[i]
and memoryF32[i-1]
). Cache effects may play a role in determining the performance difference between the two approaches.Alternatives
If you're interested in exploring alternative approaches or testing different scenarios, consider the following:
Feel free to ask if you'd like me to elaborate on any of these points!