Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object isEmpty: for-in vs Object.keys
(version: 1)
Compares performance of using for-in vs Object.keys for checking emptiness of an object.
Comparing performance of:
for-in vs Object.keys vs Object.values vs for-in with isOwnProperty
Created:
9 months ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const numObjects = 25; const fractionEmpty = 0.5; var objs = Array.from({ length: numObjects }, () => { if (Math.random() < fractionEmpty) return {}; const numKeys = 3 + Math.floor(Math.random() * 4); return Object.fromEntries(Array.from({ length: numKeys }, (_, idx) => { return [idx, Math.random()]; })); });
Tests:
for-in
outerLoop: for (const obj of objs) { for (var key in obj) { console.log(true); continue outerLoop; } console.log(false); }
Object.keys
for (const obj of objs) { const isEmpty = Object.keys(obj).length === 0 console.log(isEmpty); }
Object.values
for (const obj of objs) { const isEmpty = Object.values(obj).length === 0 console.log(isEmpty); }
for-in with isOwnProperty
outerLoop: for (const obj of objs) { for (var key in obj) { if (Object.hasOwn(obj, key)) { console.log(true); continue outerLoop; } console.log(false); } console.log(false); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for-in
Object.keys
Object.values
for-in with isOwnProperty
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Mobile/22G86 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.6
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
75942.3 Ops/sec
Object.keys
71076.9 Ops/sec
Object.values
71872.1 Ops/sec
for-in with isOwnProperty
61170.9 Ops/sec
Comments
Confirm delete:
Do you really want to delete benchmark?