var times = 65535;
function initializer(val, z) {
const i = z % 5 | 0;
return 0 == (z % 3 | 0) ? 0 === i ? "fizzbuzz" : "fizz" : 0 === i ? "buzz" : z;
}
var b = new Array(times);
for (var i = 0; i < times; i++) {
b[i] = initializer(b[i], i)
}
b
var c = [];
for (var i = 0; i < times; i++) {
c.push(initializer(c[i], i))
}
c
new Array(times).fill().map(initializer)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for i | |
for push | |
Fill Map |
Test name | Executions per second |
---|---|
for i | 167.6 Ops/sec |
for push | 153.6 Ops/sec |
Fill Map | 1302.7 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to compare three different approaches for initializing an array with a specific value in JavaScript:
for
looppush()
method to add elements to an arrayfill()
and map()
methodsScript Preparation Code
The script preparation code defines a function initializer(val, z)
that takes two arguments: val
(the value to be assigned) and z
(a counter variable used for testing). The function returns a string result based on certain conditions.
function initializer(val, z) {
const i = z % 5 | 0;
return 0 == (z % 3 | 0) ? 0 === i ? "fizzbuzz" : "fizz" : 0 === i ? "buzz" : z;
}
The function uses bitwise operators to generate a sequence of strings ("fizz", "buzz", or an empty string), which are then used as the array initialization values.
HTML Preparation Code
There is no HTML preparation code provided, so we can assume that this benchmark only concerns JavaScript execution performance.
Test Cases
There are three test cases:
**: This test case initializes an array using a
forloop and assigns each element the result of calling
initializer()with the array index
i`.**: Similar to the first test, but uses the
push()` method to add elements to the array.**: This test case initializes an empty array using the
fill()method and then maps each element to a new value by calling
initializer()`.Library
None of the provided benchmark code relies on any external libraries.
Special JS Features or Syntax
The benchmark uses some interesting features:
|
, %
) are used in the script preparation code.(x | y)
expression performs a bitwise OR operation, which returns y
if both operands are non-zero. This is equivalent to saying "if either operand is true, return the other operand".(x % y)
expression calculates the remainder of x
divided by y
.Pros and Cons
Here's a brief summary of each approach:
**: Fastest (likely due to direct access to array elements). However, it may have performance overhead due to the loop variable
i`.: Similar speed to the first test, but with additional overhead due to the method call (
push()`).Other Alternatives
If you wanted to compare other approaches for array initialization, you could consider:
Array.prototype.fill()
, which would eliminate the need for a separate mapping step.Float64Array
).Keep in mind that these alternatives might introduce additional complexity or overhead, so it's essential to carefully evaluate their performance impact.