Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Initialization: for, for push, fill map
(version: 0)
Comparing performance of:
for i vs for push vs Fill Map
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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; }
Tests:
for i
var b = new Array(times); for (var i = 0; i < times; i++) { b[i] = initializer(b[i], i) } b
for push
var c = []; for (var i = 0; i < times; i++) { c.push(initializer(c[i], i)) } c
Fill Map
new Array(times).fill().map(initializer)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for i
for push
Fill Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for i
167.6 Ops/sec
for push
153.6 Ops/sec
Fill Map
1302.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. Using a `for` loop 2. Using the `push()` method to add elements to an array 3. Using the `fill()` and `map()` methods **Script 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. ```javascript 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: 1. **"for i"`**: This test case initializes an array using a `for` loop and assigns each element the result of calling `initializer()` with the array index `i`. 2. **"for push"`**: Similar to the first test, but uses the `push()` method to add elements to the array. 3. **"Fill Map"`**: 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: * Bitwise operators (`|`, `%`) are used in the script preparation code. * The `(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". * The `(x % y)` expression calculates the remainder of `x` divided by `y`. **Pros and Cons** Here's a brief summary of each approach: 1. **"for i"`**: Fastest (likely due to direct access to array elements). However, it may have performance overhead due to the loop variable `i`. 2. **"for push"`: Similar speed to the first test, but with additional overhead due to the method call (`push()`). 3. **"Fill Map"`: Slowest ( likely due to the unnecessary mapping step). However, this approach has the advantage of using a single method call. **Other Alternatives** If you wanted to compare other approaches for array initialization, you could consider: * Using `Array.prototype.fill()`, which would eliminate the need for a separate mapping step. * Employing a more advanced data structure, such as a typed array (e.g., `Float64Array`). * Optimizing the script preparation code using techniques like tail recursion or memoization. Keep in mind that these alternatives might introduce additional complexity or overhead, so it's essential to carefully evaluate their performance impact.
Related benchmarks:
for vs map
for vs map to fill array
flatmap
JavaScript Array.prototype.map vs loop+Array.prototype.push ( big array )
Map creation in JS
Comments
Confirm delete:
Do you really want to delete benchmark?