Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unshift vs reverse push reverse
(version: 0)
Comparing performance of:
unshift vs reverse push reverse
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id=''></div>
Script Preparation code:
var array = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
Tests:
unshift
array.unshift(123)
reverse push reverse
array.reverse().push(123)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
unshift
reverse push reverse
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
unshift
68210.9 Ops/sec
reverse push reverse
3838.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and test cases for you. **Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The goal is to compare the performance of different approaches in JavaScript. **Benchmark Definition JSON** The provided benchmark definition JSON represents a single benchmark with two test cases: * "unshift vs reverse push reverse" (main benchmark) * Two individual test cases: "unshift" and "reverse push reverse " **Main Benchmark** The main benchmark compares three options for adding an element to the end of an array: 1. `array.unshift(123)` 2. `array.push(123)` 3. `array.reverse().push(123)` These three approaches are being compared in terms of their execution time. **Individual Test Cases** Each individual test case is a separate benchmark that tests one specific approach: * "unshift": Tests the performance of adding an element to the beginning of an array using `array.unshift(123)` * "reverse push reverse ": Tests the performance of reversing the array, pushing an element onto it, and then reversing it again using `array.reverse().push(123)` **Libraries and Special JavaScript Features** None of the test cases use any specific libraries or special JavaScript features beyond standard ECMAScript syntax. **Options Compared** The three options being compared in the main benchmark are: 1. **`unshift()`**: Adds an element to the beginning of an array. * Pros: O(1) amortized, efficient for small arrays. * Cons: Can be slow for large arrays due to shifting elements. 2. **`push()`**: Adds an element to the end of an array. * Pros: Simple and efficient for most cases. * Cons: May not perform well when dealing with large arrays or performance-critical code. 3. **`reverse().push()`**: Reverses the array, pushes an element onto it, and then reverses it again. * Pros: Suitable for certain use cases where reversing the array is necessary. * Cons: Inefficient due to the extra reversal operation. **Other Alternatives** There are other ways to add elements to an array in JavaScript: 1. `concat()`: Concatenates two arrays using the spread operator (`[...array].concat(element)`). 2. `Array.prototype.set()` or `TypedArray.prototype.set()`: Sets a value at a specific index. 3. Custom implementations, such as using `Buffer` or other low-level data structures. Keep in mind that these alternatives may have different performance characteristics and use cases compared to the options being tested. **Execution Results** The latest benchmark result shows two test runs with execution times: 1. "unshift" (Chrome 95 on Desktop): * Executions per second: approximately 20,193. 2. "reverse push reverse " (Chrome 95 on Desktop): * Executions per second: approximately 1,636. These results indicate that `array.unshift(123)` is significantly faster than `array.reverse().push(123)`, likely due to the efficiency of `unshift()` and the inefficiency of the reversal operation in `reverse.push()`.
Related benchmarks:
using .length within and out of for loop
Array: spread operatorasfasfzxc vs push
mapvalues reduce
unique elements in array using filter - large array
Comments
Confirm delete:
Do you really want to delete benchmark?