Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
pedroac Fizz Buzz array allocation
(version: 1)
Comparing performance of:
push vs [i-1] vs new Array vs result.length
Created:
4 years ago
by:
Registered User
Jump to the latest result
Tests:
push
const fizzBuzz = function(n) { const result = []; for (let i = 1; i <= n; ++i) { if (i % 15 === 0) { result.push('FizzBuzz'); } else if (i % 3 === 0) { result.push('Fizz'); } else if (i % 5 === 0) { result.push('Buzz'); } else { result.push(i.toString()); } } return result; }; fizzBuzz(100);
[i-1]
const fizzBuzz = function(n) { const result = []; for (let i = 1; i <= n; ++i) { if (i % 15 === 0) { result[i-1] = 'FizzBuzz'; } else if (i % 3 === 0) { result[i-1] = 'Fizz'; } else if (i % 5 === 0) { result[i-1] = 'Buzz'; } else { result[i-1] = i.toString(); } } return result; }; fizzBuzz(100);
new Array
const fizzBuzz = function(n) { const result = new Array(n); for (let i = 1; i <= n; ++i) { if (i % 15 === 0) { result[i-1] = 'FizzBuzz'; } else if (i % 3 === 0) { result[i-1] = 'Fizz'; } else if (i % 5 === 0) { result[i-1] = 'Buzz'; } else { result[i-1] = i.toString(); } } return result; }; fizzBuzz(100);
result.length
const fizzBuzz = function(n) { const result = []; result.length = n; for (let i = 1; i <= n; ++i) { if (i % 15 === 0) { result[i-1] = 'FizzBuzz'; } else if (i % 3 === 0) { result[i-1] = 'Fizz'; } else if (i % 5 === 0) { result[i-1] = 'Buzz'; } else { result[i-1] = i.toString(); } } return result; }; fizzBuzz(100);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
push
[i-1]
new Array
result.length
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided benchmark is designed to measure the performance of different ways of creating and populating an array in JavaScript. **Options being compared:** 1. **Array literal syntax**: `const result = []` 2. **Constructor function syntax**: `new Array(n)` 3. **`push()` method**: `result.push(i-1)` 4. **Pre-allocation with `length` property**: `result.length = n` **Pros and Cons of each approach:** 1. **Array literal syntax**: * Pros: Simple, concise, and widely supported. * Cons: May incur overhead due to unnecessary array creation. 2. **Constructor function syntax**: * Pros: Provides more control over the array's behavior (e.g., setting initial values). * Cons: Less common and may require explicit type casting. 3. **`push()` method**: * Pros: Efficient for adding single elements, as it avoids unnecessary memory allocation. * Cons: May incur overhead due to repeated `push()` calls, which can lead to memory fragmentation. 4. **Pre-allocation with `length` property**: * Pros: Reduces the number of array allocations, resulting in better performance. * Cons: Requires manual pre-allocation and may not be suitable for all scenarios. **Library usage:** None of the benchmark cases use any external libraries. **Special JS features or syntax:** 1. **`i-1`**: The test case `"[i-1]"` uses a clever indexing trick to access array elements without incrementing the index variable `i`. This is done by subtracting 1 from `i` and using the result as the index. 2. **Type coercion**: In the constructor function syntax, the `new Array(n)` call involves type coercion (e.g., `n` being coerced to a number), which may incur additional overhead. **Other alternatives:** * **Spread operator (`...`)**: Not used in any of the benchmark cases. * **Array.from()**: Not used in any of the benchmark cases. * **Object creation with `{}`**: Not used in any of the benchmark cases. The benchmark results provide insight into which approach is most efficient for this specific use case. The `push()` method and pre-allocation with `length` property seem to offer good performance, while the array literal syntax incurs more overhead.
Related benchmarks:
array vs hashmap
array vs hashmap 2
array vs hashmap vs array-polling
array vs hashmap1 vs array-polling
Array.filter vs. raw for-loop vs. for..of vs. Array.reduce vs. generator spread
Comments
Confirm delete:
Do you really want to delete benchmark?