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
}));
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]);
};
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;
};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object based | |
Array based |
Test name | Executions per second |
---|---|
Object based | 274453888.0 Ops/sec |
Array based | 266871152.0 Ops/sec |
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:
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:
Cons:
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:
Cons:
oldAnnotations
arrayLibrary 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:
Benchmark Results
The latest benchmark result shows:
Please note that these results are specific to the provided benchmark test case and may not reflect real-world performance differences between these approaches.