Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
compare shallow vs reference shallow
(version: 1)
Comparing performance of:
Checks before shallow vs Just Shallow
Created:
6 months ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here--> <script src='https://unpkg.com/fast-equals@5.3.2/dist/min/index.js'></script>
Script Preparation code:
var sameObjRef = { id: 1, name: "test" }; var sameArrRef = [1, 2, 3]; var testCases = { // Case 1: Primitive values (should hit fast path) sameNumber: { previous: 42, current: 42 }, differentNumbers: { previous: 42, current: 43 }, sameString: { previous: "hello", current: "hello" }, differentStrings: { previous: "hello", current: "world" }, // Case 2: null/undefined values bothNull: { previous: null, current: null }, bothUndefined: { previous: undefined, current: undefined }, nullVsUndefined: { previous: null, current: undefined }, nullVsObject: { previous: null, current: { a: 1 } }, undefinedVsObject: { previous: undefined, current: { a: 1 } }, // Case 3: Same object references (should hit fast path) sameObjectRef: { previous: sameObjRef, current: sameObjRef }, sameArrayRef: { previous: sameArrRef, current: sameArrRef }, // Case 4: Different objects with same content (requires shallowEqual) differentObjectsSameContent: { previous: { id: 1, name: "test" }, current: { id: 1, name: "test" } }, differentArraysSameContent: { previous: [1, 2, 3], current: [1, 2, 3] }, // Case 5: Different objects with different content differentObjects: { previous: { id: 1, name: "test" }, current: { id: 2, name: "different" } }, differentArrays: { previous: [1, 2, 3], current: [4, 5, 6] }, // Case 6: Nested objects (shallowEqual won't catch deep differences) nestedObjectsSame: { previous: { a: 1, b: sameObjRef }, current: { a: 1, b: sameObjRef } }, nestedObjectsDifferent: { previous: { a: 1, b: { nested: true } }, current: { a: 1, b: { nested: false } } }, // Case 7: Mixed types objectVsArray: { previous: { 0: 1, 1: 2 }, current: [1, 2] }, numberVsStringNumber: { previous: 42, current: "42" } };
Tests:
Checks before shallow
const {shallowEqual} = window['fast-equals']; function optimizedComparison(prevVal, newVal) { let normalizedPrevious = prevVal.current ?? undefined; let normalizedNew = newVal.current ?? undefined; let areValuesEqual; if (normalizedPrevious === normalizedNew) { areValuesEqual = true; } else if (normalizedPrevious == null || normalizedNew == null) { areValuesEqual = false; } else { areValuesEqual = shallowEqual(normalizedPrevious, normalizedNew); } return areValuesEqual; } Object.entries(testCases).forEach(([testName, testCase]) => { // Set up the refs for this test case let previousValueRef = {current: testCase.previous}; let newValueRef = {current: testCase.current}; optimizedComparison(previousValueRef, newValueRef) });
Just Shallow
const {shallowEqual} = window['fast-equals']; function originalComparison(prevVal, newVal) { return shallowEqual(prevVal.current ?? undefined, newVal.current ?? undefined); } Object.entries(testCases).forEach(([testName, testCase]) => { // Set up the refs for this test case let previousValueRef = {current: testCase.previous}; let newValueRef = {current: testCase.current}; originalComparison(previousValueRef, newValueRef) });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Checks before shallow
Just Shallow
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Checks before shallow
1251322.0 Ops/sec
Just Shallow
1254725.2 Ops/sec
Related benchmarks:
Check empty variable
Check empty variable
typeof 1 vs typeof 2
dolittle mark
array.includes or logical or
Lodash.get vs Lodash.property vs native test
Boolean vs !! fork
if undefined comparison
array.includes vs if &&
Comments
Confirm delete:
Do you really want to delete benchmark?