Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test performance aaaaa
(version: 0)
Comparing performance of:
optimize 1 vs optimize 2
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const input = [ undefined, // empty data { hash: "a", amount: 1 }, undefined, // empty data { hash: "c", amount: 1 }, { hash: "b", amount: 3 }, undefined, // empty data { hash: "d", amount: 1 }, { hash: "b", amount: 1 }, { hash: "a", amount: 1 }, undefined, // empty data { hash: "f", amount: 3 }, { hash: "c", amount: 1 }, { hash: "d", amount: 1 }, { hash: "a", amount: 1 }, undefined, // empty data { hash: "e", amount: 3 }, { hash: "c", amount: 1 }, ]; function deferPromise(time) { new Promise((resolve) => setTimeout(resolve, time)); }
Tests:
optimize 1
async function main() { const arr = []; const mapHashByIndex = {}; let countBreaker = 0; await new Promise((resolve) => { input.forEach((item, index) => { console.log(typeof item, item); // break if empty data if (typeof item === "undefined") { countBreaker += 1; if (index === input.length - 1) resolve(); return; } // hash not exists in arr if (typeof mapHashByIndex[item.hash] === "undefined") { // insert to position arr[index - countBreaker] = item; mapHashByIndex[item.hash] = index - countBreaker; console.log("not found in mapHashByIndex ==> insert to " + (index - countBreaker)); } else { // this index not is first index of hash ? if (mapHashByIndex[item.hash] > index) { // insert to position = index - countBreaker arr.splice(index - countBreaker, 0, { ...arr[mapHashByIndex[item.hash]], amount: arr[mapHashByIndex[item.hash]].amount + item.amount, }); // remove position arr.splice(mapHashByIndex[item.hash], 1); // incr countBreaker countBreaker += 1; console.log( "found in mapHashByIndex, repalce index from " + mapHashByIndex[item.hash] + " to " + (index - countBreaker) ); // update new map mapHashByIndex[item.hash] = index - countBreaker; } else { arr[mapHashByIndex[item.hash]].amount += item.amount; console.log("found in mapHashByIndex, update amount of " + mapHashByIndex[item.hash]); // incr countBreaker countBreaker += 1; } } if (index === input.length - 1) resolve(); }); }); console.log(arr); } main();
optimize 2
async function main() { const arr = []; const mapHashByIndex = {}; let countBreaker = 0; await new Promise((resolve) => { input.forEach((item, index) => { console.log(typeof item, item); // break if empty data if (typeof item === "undefined") { countBreaker += 1; if (index === input.length - 1) resolve(); return; } // hash not exists in arr if (typeof mapHashByIndex[item.hash] === "undefined") { // insert to position arr[index - countBreaker] = item; mapHashByIndex[item.hash] = index - countBreaker; console.log("not found in mapHashByIndex ==> insert to " + (index - countBreaker)); } else { // this index not is first index of hash ? if (mapHashByIndex[item.hash] > index) { // incr countBreaker countBreaker += 1; // insert to position = index - countBreaker arr = arr.slice(0, index - countBreaker) concat([{ ...arr[mapHashByIndex[item.hash]], amount: arr[mapHashByIndex[item.hash]].amount + item.amount, }]) .concat(arr.slice(index - countBreaker, mapHashByIndex[item.hash])) .concat(arr.slice(mapHashByIndex[item.hash], arr.length)); console.log( "found in mapHashByIndex, repalce index from " + mapHashByIndex[item.hash] + " to " + (index - countBreaker) ); // update new map mapHashByIndex[item.hash] = index - countBreaker; } else { arr[mapHashByIndex[item.hash]].amount += item.amount; console.log("found in mapHashByIndex, update amount of " + mapHashByIndex[item.hash]); // incr countBreaker countBreaker += 1; } } if (index === input.length - 1) resolve(); }); }); console.log(arr); } main();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
optimize 1
optimize 2
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 provided benchmark and explain what's being tested, compared, and their pros/cons. **Benchmark Definition** The benchmark is designed to measure the performance of two different approaches for optimizing an array while maintaining a hash-based index mapping. The test case involves iterating through an input array, processing each element, and updating the hash-based index mapping accordingly. **Approaches Compared** Two approaches are compared: 1. **Approach 1**: Inserts new elements into the array at the correct position based on the hash-based index mapping. If a new element is not found in the array, it's inserted at the beginning of the array. If an element is already present, its amount is updated. 2. **Approach 2**: Replaces the entire array with a new array that contains all elements from the original array, except for the current element being processed. This approach uses `Array.prototype.slice()` and `Array.prototype.concat()`. 3. **Raw UA String**: The benchmark reports the raw User Agent string of the browser running the test, as well as other metadata (browser version, device platform, operating system). **Performance Comparison** The benchmark measures the number of executions per second for each approach. Approach 1 outperforms Approach 2 in terms of execution speed. **Pros and Cons** Here's a brief summary: * **Approach 1**: + Pros: Simple, efficient, and optimized for insertion operations. + Cons: May incur additional overhead due to hash-based index mapping updates. * **Approach 2**: + Pros: Can be more cache-friendly and reduces memory allocation overhead. + Cons: More complex and may incur higher CPU overhead due to repeated `Array.prototype.slice()` and `Array.prototype.concat()` operations. In conclusion, Approach 1 appears to be the most efficient approach for this specific use case, while Approach 2 offers potential performance benefits at the cost of added complexity.
Related benchmarks:
Factory
Time to create simple promises
async for vs promise all (setTimeout version)
needless async impact
Restoring actual data
Comments
Confirm delete:
Do you really want to delete benchmark?