Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Testing array normalizastion2
(version: 0)
Comparing performance of:
Object based vs Array based
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
Array.from(Array(100).keys()).map((i) => ({ id: i+1, name: 'Bar', asd: 123123 })); Array.from(Array(50).keys()).map((i) => ({ id: i+5, title: 'Bar', age: 12 }));
Tests:
Object based
const normalizeLiveUpdateData = ( newData, oldaData ) => { if (!oldAnnotations) return newAnnotations; const ids = []; const data = {}; oldAnnotations.forEach((a) => { ids.push(a.id); data[a.id] = a; }); newAnnotations.forEach((a) => { if (ids.indexOf(a.id) === -1) { ids.push(a.id); } data[a.id] = a; }); return ids.map(id => data[id]); };
Array based
const normalizeLiveUpdateData = ( newAnnotations, oldAnnotations) => { if (!oldAnnotations) return newAnnotations; let normalizedAnnotations = oldAnnotations; newAnnotations.forEach((newAnnotation) => { const existingAnnotation = oldAnnotations.find((oldAnnotation) => oldAnnotation.id === newAnnotation.id); if (existingAnnotation) { normalizedAnnotations = normalizedAnnotations.map(obj => newAnnotations.find(o => o.id === obj.id) || obj); } else { normalizedAnnotations.push(newAnnotation); } }); return normalizedAnnotations; };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object based
Array based
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):
**Benchmark Overview** The provided JSON represents a benchmark test case for normalizing live update data, specifically comparing two approaches: object-based and array-based normalization. **Script Preparation Code** The script preparation code generates sample data: * `Array.from(Array(100).keys()).map((i) => ({ id: i+1, name: 'Bar', asd: 123123 }));`: Creates an array of 100 objects with unique IDs (2-101), a fixed "name" property, and a random "asd" value. * `Array.from(Array(50).keys()).map((i) => ({ id: i+5, title: 'Bar', age: 12 }));`: Creates an array of 50 objects with unique IDs (6-55), a fixed "title" property, and a fixed "age" value. **Individual Test Cases** There are two test cases: ### Object-Based Approach ```javascript const normalizeLiveUpdateData = ( newData, oldaData ) => { // ... }; ``` This approach iterates over the `oldAnnotations` array, adding new annotations to an object that maps IDs to annotation objects. Then, it iterates over the `newAnnotations` array and updates the existing annotation objects in the map. Pros: * Easy to understand and maintain * Does not require explicit loop management Cons: * May lead to performance issues due to frequent object lookups * Can result in slower execution times for large datasets ### Array-Based Approach ```javascript const normalizeLiveUpdateData = ( newAnnotations, oldAnnotations ) => { // ... }; ``` This approach iterates over the `newAnnotations` array and updates the existing annotation objects in the `oldAnnotations` array. If a new annotation has an ID that does not exist in `oldAnnotations`, it is appended to the end of the array. Pros: * Can result in faster execution times due to reduced object lookups * Does not require explicit loop management Cons: * Requires additional memory for storing the updated `oldAnnotations` array * May lead to slower initialization times due to array resizing **Library and Special Features** None of the provided code snippet indicates any specific JavaScript libraries or special features, such as async/await, Promises, or ES6+ syntax. **Alternatives** Other approaches for normalizing live update data could include: 1. Using a hybrid approach that combines elements of both object-based and array-based methods. 2. Utilizing a data structure like a hash table or a graph to store annotations, which can provide efficient lookup and insertion operations. 3. Implementing an incremental update mechanism that only updates existing annotations, rather than re-normalizing the entire dataset. **Benchmark Results** The latest benchmark result shows: * The "Object-Based" approach executing approximately 107.5 million times per second. * The "Array-Based" approach executing approximately 26.7 million times per second on a Safari 14 browser on a Mac OS X 11.0 system. Please note that these results are specific to the provided benchmark test case and may not reflect real-world performance differences between these approaches.
Related benchmarks:
demo21312312312
adsfadsffads
Array range generating
Array.from or map
Array.from vs new Array using index value
Comments
Confirm delete:
Do you really want to delete benchmark?