Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
loops_and_stuff
(version: 0)
Comparing performance of:
while loop vs uncached length vs cached length postfix vs cached length prefix
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [1, 2, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20, 77, "hu", 92, 18, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20, 77, "hu", 92, 18, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20, 77, "hu", 92, 18, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20, 77, "hu", 92, 18, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20, 77, "hu", 92, 18, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20, 77, "hu", 92, 18]; var tmp;
Tests:
while loop
var len = arr.length; while(len--) { tmp = arr[len]; }
uncached length
for (let i = 0; i < arr.length; i++) { tmp = arr[i]; }
cached length postfix
for (let i = 0, len = arr.length; i < len; i++) { tmp = arr[i]; }
cached length prefix
for (let i = 0, len = arr.length; i < len; ++i) { tmp = arr[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
while loop
uncached length
cached length postfix
cached length prefix
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 dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark measures the performance of different loop constructs in JavaScript: while loops, uncached length for loops, cached length prefix for loops, and cached length postfix for loops. The benchmark script generates an array `arr` with 40 elements and uses it to iterate through the array using each of these loop constructs. **Loop Constructs** 1. **While Loop**: This loop construct uses a while loop that decrements the index `len` until it reaches 0, accessing the element at the current index in the array. ```javascript while(len--) { tmp = arr[len]; } ``` Pros: Simple to implement and works well for small arrays. Cons: Since the length of the array is not cached, it requires a constant number of operations to retrieve the length. This can lead to slower performance for large arrays. 2. **Uncached Length For Loop**: This loop construct uses a traditional for loop that accesses the length of the array using `arr.length` and then decrements the index. ```javascript for (let i = 0; i < arr.length; i++) { tmp = arr[i]; } ``` Pros: This approach is similar to the while loop, but with the added benefit of being a traditional for loop. Cons: Since the length of the array is not cached, it requires a constant number of operations to retrieve the length. This can lead to slower performance for large arrays. 3. **Cached Length Prefix For Loop**: This loop construct uses a for loop that accesses the length of the array using `len = arr.length` and then decrements the index. ```javascript for (let i = 0, len = arr.length; i < len; i++) { tmp = arr[i]; } ``` Pros: By caching the length in a variable, this approach reduces the number of operations required to access the array. Cons: The prefix notation can be less readable than the postfix notation. 4. **Cached Length Postfix For Loop**: This loop construct uses a for loop that accesses the length of the array using `len = arr.length` and then decrements the index in a postfix notation. ```javascript for (let i = 0, len = arr.length; i < len; ++i) { tmp = arr[i]; } ``` Pros: By caching the length in a variable and using a postfix notation, this approach is similar to the cached length prefix for loop but with less readability. Cons: The postfix notation can be less readable than the prefix notation. **Library Used** None of the provided benchmark definitions use any external libraries. However, the script uses a temporary variable `tmp` to store the array elements during iteration. **Special JS Features or Syntax** The benchmark defines four different loop constructs, but none of them require special JavaScript features or syntax beyond the standard language features available in modern browsers.
Related benchmarks:
50000 number array(natvie forEach vs lodash forEach)
splice vs length
splice vs length 2
Array filter versus for loop
Comments
Confirm delete:
Do you really want to delete benchmark?