Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
operator `in` vs. get property by key
(version: 0)
Comparing performance of:
if (key in obj) vs if (object[key])
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var list = Array.from({ length: 1_000_000 }, _ => crypto.randomUUID()); var object = Object.create(null); var x; list.toReversed().forEach((i, idx) => { if (idx % 2) { object[i] = [0x1]; } });
Tests:
if (key in obj)
list.forEach((key) => { if (key in object) { x = object[key]; } });
if (object[key])
list.forEach((key) => { if (object[key]) { x = object[key]; } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
if (key in obj)
if (object[key])
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
if (key in obj)
2.9 Ops/sec
if (object[key])
2.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explore what's being tested. **Overview** The benchmark compares two approaches to check if a key exists in an object: 1. Using the `in` operator (`if (key in obj)`). 2. Using property access with a bracket notation (`if (object[key])`). **Script Preparation Code** The script prepares an array of 1 million random strings and creates an empty object with no prototype chain. It then populates the object with values at specific indices, where every other index is assigned a value. **Html Preparation Code** There is no HTML preparation code provided, which means that this benchmark only tests the JavaScript engine's performance in a headless environment. **Individual Test Cases** The two test cases are identical except for the syntax used to access the object: 1. `if (key in obj)` 2. `if (object[key])` **Library and Purpose** There is no explicit library mentioned, but it's likely that the benchmark relies on some internal JavaScript features or engines' optimizations. **Special JS Features/Syntax** The use of the `_` function to create an array of 1 million random strings is a clever way to generate data. This syntax is not specific to any particular library or standard, but rather a JavaScript feature that allows for simple and efficient array creation. **Options Compared** The benchmark compares two approaches: 1. Using the `in` operator: `if (key in obj)` 2. Using property access with bracket notation: `if (object[key])` **Pros and Cons** Using the `in` operator has several advantages: * It is more readable and concise. * It can work with objects that don't have a prototype chain, as it only checks if the key exists directly in the object. However, using property access with bracket notation has some benefits: * It can be faster because it avoids the overhead of the `in` operator. * It allows for more control over the iteration process, as you can add additional logic to handle different cases. In this benchmark, both approaches seem to have similar performance characteristics, which is not surprising given that they are essentially equivalent in terms of syntax and semantics. However, the `in` operator might incur some overhead due to its implementation in the JavaScript engine, while bracket notation might be faster due to its direct access to the object. **Other Alternatives** There are other ways to check if a key exists in an object: 1. Using `hasOwnProperty(key)` for objects with a prototype chain. 2. Using `Object.keys().includes(key)` or `Array.prototype.includes()` for modern browsers. 3. Using a custom implementation, such as using `for...in` loop and checking the iterator's value. However, these alternatives are not being tested in this benchmark, which only compares two specific approaches: `in` operator and property access with bracket notation.
Related benchmarks:
For in vs For of
Object.entries vs Object.keys vs for...in
Object.fromEntries vs reduce vs property assignment vs Map
For in vs Object.entries
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?