Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
keys vs new array
(version: 0)
Comparing performance of:
keys vs array vs set
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1 };
Tests:
keys
const arr = Object.keys(obj)
array
const arr = ["a", "b", "c", "d", "e", "f", "g"]
set
const arr = new Set(Object.keys(obj))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
keys
array
set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
keys
10525721.0 Ops/sec
array
174116896.0 Ops/sec
set
4526588.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested. **Benchmark Definition** The benchmark definition represents a JavaScript microbenchmark that compares three different approaches to create an array from the keys of an object: 1. `const arr = Object.keys(obj)` 2. `const arr = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\"]` (manually creating the array) 3. `const arr = new Set(Object.keys(obj))` **Options Comparison** The three options are being compared to measure their performance differences. * **Option 1: Using `Object.keys()`** + Pros: - Simple and straightforward - Works with any object (not just strings) + Cons: - May be slower due to the overhead of calling a function on an object - Can be less efficient if the object is large, as it needs to iterate over all its properties * **Option 2: Manually creating the array** + Pros: - Faster and more efficient, as it avoids the overhead of a function call + Cons: - Requires manual effort to create the array, which can lead to errors if not done correctly - Not suitable for all use cases (e.g., when working with large objects) * **Option 3: Using `Set`** + Pros: - More efficient than `Object.keys()`, as it uses a hash table to store the keys - Can be faster for large objects, as it avoids iterating over all properties + Cons: - May not work as expected if the object contains non-string keys (not supported by `Set`) - Requires creating a new `Set` object, which can be slower than directly using an array **Library: `Object.keys()`** The `Object.keys()` method is a built-in JavaScript method that returns an array of strings containing all the properties found directly in the specified object. Its purpose is to provide a convenient way to iterate over an object's keys. **Special JS Feature/Syntax: None** There are no special features or syntax used in this benchmark. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. Using `enum` (if available in your target browser): `const arr = Object枚名(obj)` 2. Using a library like `lodash.keys()` or `p-obj-keys()` 3. Creating the array manually using loops and indexing (e.g., `const arr = []; for (const key in obj) { arr.push(key); }`) 4. Using a different data structure, such as a `Map` or a custom object with keys. Keep in mind that these alternatives might not be relevant to this specific benchmark or your use case.
Related benchmarks:
For in vs For of
Object.keys vs Object.values
Object.keys() vs _.key()
Cached Object.keys() vs inline Object.keys()
Comments
Confirm delete:
Do you really want to delete benchmark?