Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.create(null) vs {} unknown property
(version: 0)
Comparing performance of:
Object.create(null); vs {} vs key in Object.create(null) vs key in {}
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Object.create(null);
const obj = Object.create(null); const result = []; for (i = 0; i < 100_000; i++) { result.push(obj[i]); }
{}
const obj = {}; const result = []; for (i = 0; i < 100_000; i++) { result.push(obj[i]); }
key in Object.create(null)
const obj = Object.create(null); const result = []; for (i = 0; i < 100_000; i++) { result.push(i in obj); }
key in {}
const obj = {}; const result = []; for (i = 0; i < 100_000; i++) { result.push(i in obj); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object.create(null);
{}
key in Object.create(null)
key in {}
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.create(null);
406.7 Ops/sec
{}
353.6 Ops/sec
key in Object.create(null)
405.1 Ops/sec
key in {}
336.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is tested:** The provided benchmark compares the performance of three approaches: 1. `Object.create(null)`: This creates an object without any prototype chain, effectively making it a "null" object. It's used to test the performance of accessing properties on such an object. 2. `{}` (empty object literal): This creates a normal JavaScript object with no inherent properties. 3. `key in Object.create(null)` and `key in {}`: These expressions are used to test if a property exists on each of these objects. **Options compared:** * **Object create null vs {}**: The main difference between these two approaches is how they handle unknown (or non-existent) properties. When using `Object.create(null)`, attempting to access an unknown property throws a `TypeError`. In contrast, when using `{}`, the same property lookup will simply return `undefined`. * **Key in Object.create(null) vs key in {}**: The main difference here is that when using `key in Object.create(null)`, it will always throw a `TypeError` if the key is unknown, whereas with `{}`, the result will be `false`. **Pros and cons:** * Using `Object.create(null)` has a clear security benefit, as it makes it more difficult for malicious code to access potentially sensitive properties. * However, this approach can introduce performance overhead due to the need to check if a property exists or throw an error when accessed. * Using `{}` is generally faster and more convenient but may expose potential vulnerabilities if not used carefully. **Library/dependency:** None of these benchmarks explicitly use any external libraries or dependencies. The only library being used here is `Object`, which is a built-in JavaScript object. **Special JS feature/syntax:** None of the test cases rely on special JavaScript features like async/await, Promises, or web workers. **Other alternatives:** * For testing object creation and property access performance, one might also consider benchmarking other approaches, such as using `Object.assign()` or `Object.fromEntries()`. * If security is a top concern, additional benchmarks could focus on testing the performance of different encryption methods or secure coding practices. In summary, this benchmark provides a useful comparison between three common approaches to handling unknown properties in JavaScript. While it's primarily focused on performance, understanding the trade-offs and implications of each approach can help developers make more informed decisions about their code.
Related benchmarks:
object create vs others
Object.create vs Object literal
Object.create(null) vs Object literal
javascript new vs Object.create 2
javascript new vs Object.create 3
Comments
Confirm delete:
Do you really want to delete benchmark?