Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Object isEmpty: for-in vs Object.keys
Compares performance of using for-in vs Object.keys for checking emptiness of an object.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:141.0) Gecko/20100101 Firefox/141.0
Browser:
Firefox 141
Operating system:
Windows
Device Platform:
Desktop
Date tested:
9 months ago
Test name
Executions per second
for-in
12359.1 Ops/sec
Object.keys
13025.4 Ops/sec
Object.values
12934.7 Ops/sec
for-in with isOwnProperty
12437.6 Ops/sec
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); }