Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
New obj vs not
(version: 0)
Comparing performance of:
New object vs Mutating object
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
const newerValue = 'newer-value'; const olderValue = 'older-value'; const unchangedValue = 'unchanged-value'; const newerTimestamp = '200000000000000:00000:my-client:v01'; const olderTimestamp = '10000000000000:00000:my-client:v01'; const unchangedTimestamp = '00000000000000:00000:my-client:v01'; var newPatch = { inspection: { name: newerValue, note: olderValue, timestamps: { name: newerTimestamp, note: olderTimestamp, }, areas: [{ id: '1234', name: newerValue, note: olderValue, timestamps: { name: newerTimestamp, note: olderTimestamp, }, items: [{ id: '1234', name: newerValue, note: olderValue, timestamps: { name: newerTimestamp, note: olderTimestamp, }, }, ], }, ], }, }; var existingPatch = { inspection: { name: olderValue, note: newerValue, other: unchangedValue, timestamps: { name: olderTimestamp, note: newerTimestamp, other: unchangedTimestamp, }, areas: [{ id: '1234', name: olderValue, note: newerValue, other: unchangedValue, timestamps: { name: olderTimestamp, note: newerTimestamp, other: unchangedTimestamp, }, items: [{ id: '1234', name: olderValue, note: newerValue, other: unchangedValue, timestamps: { name: olderTimestamp, note: newerTimestamp, other: unchangedTimestamp, }, }, { id: 'unchanged-item', name: unchangedValue, note: unchangedValue, other: unchangedValue, timestamps: { name: unchangedValue, note: unchangedValue, other: unchangedTimestamp, }, }, ], }, ], }, }; var compareTs = (ts1, ts2, value1, value2) => { if (!ts1 && !ts2) { return undefined; } if (!ts1) { return value2; } if (!ts2) { return value1; } const compareRes = ts1.localeCompare(ts2); if (compareRes <= 0) { return value2; } return value1; };
Tests:
New object
var mergeWithTimestampsCustomizer = ( targetValue, srcValue, key, target, source, ) => { // TODO: way to standardize uuid key if (_.isArray(targetValue)) { return _.values( _.mergeWith(_.keyBy(targetValue, 'id'), _.keyBy(srcValue, 'id'), mergeWithTimestampsCustomizer), ); } if (target?.timestamps?.[key] || source?.timestamps?.[key]) { return compareTs(target?.timestamps?.[key], source?.timestamps?.[key], targetValue, srcValue); } // TODO: way to standardize timestamps key if (key === 'timestamps') { return _.mergeWith({}, targetValue, srcValue, (v, srcV) => { return compareTs(v, srcV, v, srcV); }); } return undefined; // Fallback to lodash's merge }; const mergeWithTimestamps = (existing, newValues) => { return _.mergeWith({}, existing, newValues, mergeWithTimestampsCustomizer); }; mergeWithTimestamps(newPatch, existingPatch);
Mutating object
var mergeWithTimestampsCustomizer = ( targetValue, srcValue, key, target, source, ) => { // TODO: way to standardize uuid key if (_.isArray(targetValue)) { return _.values( _.mergeWith(_.keyBy(targetValue, 'id'), _.keyBy(srcValue, 'id'), mergeWithTimestampsCustomizer), ); } if (target?.timestamps?.[key] || source?.timestamps?.[key]) { return compareTs(target?.timestamps?.[key], source?.timestamps?.[key], targetValue, srcValue); } // TODO: way to standardize timestamps key if (key === 'timestamps') { return _.mergeWith(targetValue, srcValue, (v, srcV) => { return compareTs(v, srcV, v, srcV); }); } return undefined; // Fallback to lodash's merge }; const mergeWithTimestamps = (existing, newValues) => { return _.mergeWith(existing, newValues, mergeWithTimestampsCustomizer); }; mergeWithTimestamps(newPatch, existingPatch);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
New object
Mutating object
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 explain the benchmark in detail. **Benchmark Definition** The benchmark is testing two different approaches to merging objects: creating a new object and mutating an existing one. The `benchmark definition` json contains the script preparation code, which defines two patches: `newPatch` and `existingPatch`. These patches represent the values being merged together. The differences between them are: * `newPatch`: * `inspection.name`: newer-value * `inspection.note`: older-value * `inspection.timestamps`: newer-timestamp * One nested area with one nested item, also with newer value for name and note. * `existingPatch`: * `inspection.name`: older-value * `inspection.note`: newer-value * `inspection.timestamps`: older-timestamp * No nested areas or items. The custom merge function, `mergeWithTimestampsCustomizer`, is used to perform the merging. It uses Lodash's `mergeWith` function with a custom merge function that compares timestamps using the `compareTs` function. **Benchmark Tests** There are two benchmark tests: 1. **New object**: This test creates a new object by calling `mergeWithTimestamps(newPatch, existingPatch)`. 2. **Mutating object**: This test mutates an existing object by calling `mergeWithTimestamps(existingPatch, newPatch)`. **Comparison** The benchmark results show the performance of both approaches: * The "New object" test is faster, with approximately 4.3x more executions per second than the "Mutating object" test. * The exact execution counts are: * New object: 60007.84765625 executions/second * Mutating object: 27754.017578125 executions/second **Interpretation** The results suggest that creating a new object is faster than mutating an existing one, likely due to the overhead of copying and updating values in the original object. However, this could also depend on various factors such as: * The size of the objects being merged * The complexity of the merge function * The specifics of the comparison function (`compareTs`) It's worth noting that these results might not be representative for all scenarios and can vary depending on the specific use case.
Related benchmarks:
lodash.mapKeys vs Iterating over Object.keys
lodash.mapKeys vs Native
Object.keys() vs Object.values() vs Object.entries()
lodash isEmpty vs Vanilla JS
objects array find vs some
Comments
Confirm delete:
Do you really want to delete benchmark?