Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
xyzabc123
(version: 0)
Comparing performance of:
spread vs unshift vs new array
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
spread
const events = {}; const a = 'ad'; const c = 'c'; events[c] = [a, ...(events[c] ?? [])]
unshift
const events = {}; const a = 'ad'; const c = 'c'; events[c] = Array.isArray(events[c]) ? events[c].unshift(a) : [a]
new array
const events = {}; const a = 'ad'; const c = 'c'; events[c] = events[c] || []; events[c].unshift(a);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread
unshift
new array
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):
Let's break down the provided benchmark and explain what is being tested, compared, and the pros/cons of different approaches. **Benchmark Definition** The benchmark definition is a JSON object that provides metadata about the benchmark. In this case, it's quite minimal: * `Name`: A unique name for the benchmark. * `Description`: An empty string, suggesting that no description is provided. * `Script Preparation Code` and `Html Preparation Code`: Empty strings, indicating that no preparation code is needed. **Individual Test Cases** There are three test cases in this benchmark. Each test case has a `Benchmark Definition` string that contains the JavaScript code to be executed. The test cases compare different approaches to set up an array and then shift an element into it. Here's a breakdown of each test case: 1. **Spread**: `events[c] = [a, ...(events[c] ?? [])]` * This approach uses the spread operator (`...`) to create a new array by copying the existing elements in `events[c]` and adding `a`. If `events[c]` is empty or null, an empty array is created. 2. **Unshift**: `events[c] = Array.isArray(events[c]) ? events[c].unshift(a) : [a]` * This approach checks if the existing element in `events[c]` is an array using `Array.isArray()`. If it's an array, the new element `a` is added to the beginning of the array using `unshift()`. If not, a new array containing only `a` is created. 3. **New Array**: `events[c] = events[c] || []`; `events[c].unshift(a);` * This approach creates a new array if `events[c]` is null or undefined, and then adds the element `a` to the beginning of the array using `unshift()`. **Comparison** The benchmark compares the performance of these three approaches across different executions per second (ExecutionsPerSecond) values. The results show that: * **Unshift** consistently performs better than the other two approaches. * **Spread** is slightly slower than **New Array**, which may be due to the overhead of creating a new array using spread operator. * **New Array** provides a consistent result across different executions, but it's not the fastest approach. **Pros and Cons** Here are some pros and cons for each approach: 1. **Unshift**: * Pros: Fastest performance, as `unshift()` is an O(1) operation in most cases. * Cons: May require additional checks to ensure the element is added to the beginning of the array correctly. 2. **Spread**: * Pros: Convenient and readable code, as it uses a familiar syntax for creating new arrays. * Cons: Can be slower due to the overhead of creating a new array using spread operator. 3. **New Array**: * Pros: Provides a consistent result across different executions, as it always creates a new array. * Cons: May require additional memory allocation if the existing element is not null or undefined. **Libraries and Special Features** There are no explicit libraries mentioned in the benchmark definition or test cases. However, the use of `Array.isArray()` suggests that this implementation uses modern JavaScript features. The only special feature used here is the spread operator (`...`), which was introduced in ECMAScript 2015 (ES6). This allows for concise code and can be useful when working with arrays, objects, or other iterable data structures.
Related benchmarks:
indexOf vs while vs for emoji
Rafa speed test 1
dfjf2hdshsdrh
Seedrandom browser (+ xoshift128**)
xoshiro128** various implementations
Comments
Confirm delete:
Do you really want to delete benchmark?