Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fastest way to check if object is empty with 3 types of checks
(version: 0)
You don't need to make add for loops in your test scenarios, the benchmark does it itself.
Comparing performance of:
object.keys() vs JSON.stringify() vs iterate over props
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var xxx = {}
Tests:
object.keys()
Object.keys(xxx) === 0
JSON.stringify()
JSON.stringify(xxx) === "{}"
iterate over props
let isEmpty = true; for (const key in xxx) { isEmpty = false; break; } return isEmpty;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
object.keys()
JSON.stringify()
iterate over props
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
object.keys()
106954536.0 Ops/sec
JSON.stringify()
33201214.0 Ops/sec
iterate over props
125198416.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its various components. **Benchmark Overview** The provided benchmark measures which method is the fastest for checking if an object is empty, using three different approaches: `object.keys()`, `JSON.stringify()`, and iterating over the object's properties (`for...in` loop). **Approach 1: `object.keys()`** This approach uses the built-in `object.keys()` method to get an array of the object's property names. If the array is empty, it means the object is empty. Pros: * Simple and concise code * Works on most browsers (note that some older browsers may not support this method) Cons: * May be slower than other approaches for very large objects, as it needs to iterate over all properties **Approach 2: `JSON.stringify()`** This approach uses the `JSON.stringify()` method to convert the object to a JSON string. An empty object will result in an empty string. Pros: * Can work with nested objects and arrays * Can be faster than iterating over properties for very large objects, as it only needs to serialize the entire object Cons: * May not work correctly if the object contains non-serializable values (e.g., functions or undefined) * May be slower on some browsers due to stringification overhead **Approach 3: Iterating over properties (`for...in` loop)** This approach uses a traditional `for...in` loop to iterate over the object's properties. If any property is not empty, it sets a flag and exits the loop. Pros: * Works correctly for all objects, regardless of their size or complexity * Can be faster than other approaches for very large objects, as it only needs to iterate over non-empty properties Cons: * More verbose code compared to `object.keys()` or `JSON.stringify()` **Library Used: None** There are no external libraries used in this benchmark. **Special JavaScript Features/ Syntax: None** None of the test cases use special JavaScript features or syntax, such as async/await, Promises, or decorators. **Alternatives** Other approaches to checking if an object is empty could include: * Using a `Set` or `Map` object to check for emptiness (e.g., `new Set(Object.keys(xxx)).size === 0`) * Using a recursive function to iterate over the object's properties * Using a library like Lodash, which provides utility functions for working with objects Note that the choice of approach depends on the specific use case and requirements of the application.
Related benchmarks:
Fastest way to check if object is empty
Fastest way to check if object is empty (for in vs.
Fastest way to check if object is empty2
Fastest way to check if object is empty - EP3
Fastest way to check if object is empty using length
Comments
Confirm delete:
Do you really want to delete benchmark?