Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
legacy RW calc tests
(version: 0)
Task 1: Find a random element's position in an array. Task 2: Find a shared single random element's position in two arrays.
Comparing performance of:
Angular minds legacy vs Refactored
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = []; var arr2 = []; var arr3 = []; for (let i = 0; i < 15000; i++) { const str = (Math.random() + 1).toString(36).substring(7); arr1.push(str); arr2.push(str); arr3.push(str); } function shuffle(array) { var currentIndex = array.length, randomIndex; // While there remain elements to shuffle... while (currentIndex != 0) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex--; // And swap it with the current element. [array[currentIndex], array[randomIndex]] = [ array[randomIndex], array[currentIndex]]; } return array; } shuffle(arr2); shuffle(arr3); var random1 = arr1[Math.floor(Math.random() * arr1.length)]; var random2 = arr2[Math.floor(Math.random() * arr2.length)];
Tests:
Angular minds legacy
let arr1Index = 0; let arr2Index = 0; let arr3Index = 0; let arr1Cnt = 0; arr1.forEach((elem_1)=>{ if (elem_1 === random1) { arr1Index = arr1Cnt; } arr1Cnt++; }) let arr2Cnt = 0; arr2.forEach((elem_2)=>{ let arr3Cnt = 0; arr3.forEach((elem_3)=>{ if (elem_2 === elem_3 && elem_2 === random2 && elem_3 === random2) { arr2Index = arr2Cnt; arr3Index = arr3Cnt; } arr3Cnt++; }) arr2Cnt++; })
Refactored
let arr1Index = 0; let arr2Index = 0; let arr3Index = 0; for (const elem_1 of arr1) { if (elem_1 === random1) { break; } arr1Index++; } for (const elem_2 of arr2) { if (elem_2 === random2) { break; } arr2Index++; } for (const elem_3 of arr3) { if (elem_3 === random2) { break; } arr3Index++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Angular minds legacy
Refactored
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 what is being tested in the provided benchmark. **Benchmark Definition** The benchmark defines two test cases: 1. **Legacy**: This test case searches for a shared single random element's position in three arrays (`arr1`, `arr2`, and `arr3`). It uses a traditional loop-based approach to find the index of the common element. 2. **Refactored**: This test case is similar to the legacy test, but it uses a more modern JavaScript feature called **`for...of` loops** (also known as "forEach" with an iterator) to iterate over the arrays. **Options being compared** The benchmark compares two approaches: 1. Traditional loop-based approach (`legacy`) 2. Modern `for...of` loops (`refactored`) **Pros and Cons of each approach** **Legacy (Traditional Loop-Based Approach)** Pros: * Widely supported across older browsers * Easy to understand and implement for developers familiar with traditional loops Cons: * Can be slower due to the overhead of traditional loop logic * May not perform as well on modern JavaScript engines optimized for performance **Refactored (Modern `for...of` Loops)** Pros: * Faster execution due to optimized engine support * More concise and expressive code Cons: * Less widely supported across older browsers (may require polyfills) * Can be less intuitive for developers unfamiliar with modern JavaScript features **Library and its purpose** In the benchmark definition, `shuffle()` is a custom function used to randomize the order of elements in arrays. Its purpose is not explicitly mentioned in the provided information, but it's likely used to ensure that the test cases are run with different, yet predictable, orders. **Special JS feature or syntax** The modern `for...of` loops (`refactored`) utilize a new JavaScript feature introduced in ECMAScript 2015 (ES6). This feature allows for more concise and expressive code when working with arrays and other iterable objects. The use of this feature is meant to highlight the performance difference between traditional loop-based approaches and modern, optimized engine support. **Other alternatives** There are other alternatives that could be used to optimize or compare these two approaches: 1. **`Array.prototype.indexOf()`**: This method can be used to find the index of an element in an array. However, it may not be as performant as traditional loop-based approaches or modern `for...of` loops. 2. **`Map` data structure**: A `Map` can be used to store and retrieve elements by their key (in this case, a unique string value). This approach would require additional code and setup but could potentially provide faster execution times due to the optimized engine support for Maps. 3. **Just-In-Time (JIT) compilation**: Some JavaScript engines, like V8 in Chrome, use JIT compilation to optimize performance-critical code paths. If both test cases were optimized for JIT compilation, they might exhibit similar performance characteristics. Keep in mind that the choice of optimization approach depends on the specific requirements and constraints of the benchmarking scenario.
Related benchmarks:
Already sorted versus random
LIS-test2
set.has vs. array.includes vs obj[key] vs map.get 2
Array Find vs Some (shuffled array)
Comments
Confirm delete:
Do you really want to delete benchmark?