Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Benchmark function for list
(version: 1)
hi Yandex
Comparing performance of:
Array unshift vs Array push + reverse vs just a static array, vs recursion
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function listAutoComplite (list,n){ let i = 1; for(; i < n; i++){ list.value = i; list.next = { value : 0, next:null } list = list.next; } list.value = i; } var someList = {}; listAutoComplite(someList,10000);
Tests:
Array unshift
function reversePrint1(linkedList) { const result = []; while(linkedList.next != null){ result.unshift(linkedList.value); linkedList = linkedList.next; } result.unshift(linkedList.value); console.log(...result); } reversePrint1(someList);
Array push + reverse
function reversePrint2(linkedList) { const result = []; while(linkedList.next != null){ result.push(linkedList.value); linkedList = linkedList.next; } result.push(linkedList.value); console.log(...result.reverse()); } reversePrint2(someList);
just a static array,
function reversePrint3(linkedList) { let List = Object.assign(linkedList); let i = 0; while(List.next != null){ ++i; List = List.next; } const result = new Array (i+1); while(linkedList.next != null){ result[i] =linkedList.value linkedList = linkedList.next; i-- } result[i] = linkedList.value; console.log(...result); } reversePrint3(someList);
recursion
function reversePrint4(linkedList){ const result = []; function recursion(obj){ if(obj.next === null){ result.push(obj.value); return } recursion(obj.next); result.push(obj.value); } recursion(linkedList); console.log(...result); } reversePrint4(someList);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array unshift
Array push + reverse
just a static array,
recursion
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):
I'll break down the benchmark and its options, highlighting their pros and cons, library usage, and special JavaScript features. **Benchmark Overview** The test cases create a linked list data structure, which is then used to measure performance differences in various operations: 1. `Array unshift` 2. `Array push + reverse` 3. `just a static array` (no linked list) 4. `recursion` **Options Compared** Each option has its strengths and weaknesses: 1. **Link List with `next` Property**: This approach creates a dynamic, flexible data structure that can grow or shrink as needed. * Pros: efficient memory usage, no need to create arrays of fixed size. * Cons: more complex implementation, potential performance issues due to frequent node creation/deletion. 2. **Static Array**: A simple, fixed-size array is used instead of a linked list. * Pros: easy to implement, predictable performance. * Cons: inefficient memory usage, cannot grow or shrink dynamically. **Library Usage** None of the test cases explicitly use any external libraries, but they do utilize JavaScript's built-in `Object.assign()` method (in option 3). **Special JavaScript Features** No special JavaScript features are used in this benchmark. However, some modern browsers may have experimental or optimized implementations of certain functions that could affect performance. **Benchmark Results** The results show the number of executions per second for each test case: 1. `Array unshift`: lowest execution rate (35.99/s) 2. `Array push + reverse`: moderate execution rate (238.25/s) 3. `recursion`: highest execution rate (222.71/s) 4. `just a static array`: lowest execution rate (35.99/s), similar to `Array unshift` **Interpretation** The results suggest that the recursive approach (`recursion`) is the fastest, likely due to its simplicity and optimized implementation in modern browsers. The linked list with `next` property and static array approaches have lower execution rates, likely due to their complexity and memory usage. As a software engineer, it's essential to consider factors like memory efficiency, performance, and code complexity when designing data structures or algorithms for your project. **Other Alternatives** Some alternative approaches you might consider: * Using an existing JavaScript library like Lodash or Ramda for array operations * Implementing a hybrid data structure that combines the benefits of linked lists and static arrays * Optimizing the recursive approach using memoization, caching, or other techniques to reduce function calls
Related benchmarks:
for let
let vs var for loop
for vs reduce (find max value)
for vs reduce (find max value) i10000
Million loops
Comments
Confirm delete:
Do you really want to delete benchmark?