Object keys and values in Javascript - Time & Space Complexity
When working with objects in JavaScript, we often get their keys or values. Understanding how long this takes helps us write faster code.
We want to know how the time to get keys or values changes as the object grows.
Analyze the time complexity of the following code snippet.
const obj = {a: 1, b: 2, c: 3, d: 4};
const keys = Object.keys(obj);
const values = Object.values(obj);
console.log(keys);
console.log(values);
This code gets all the keys and values from an object and prints them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: JavaScript internally loops over each property in the object to collect keys or values.
- How many times: Once for each property in the object.
As the object gets bigger, the time to get keys or values grows in a straight line with the number of properties.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to collect keys or values |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The work grows evenly as the object size grows.
Time Complexity: O(n)
This means the time to get all keys or values grows directly with the number of properties in the object.
[X] Wrong: "Getting keys or values is instant no matter how big the object is."
[OK] Correct: JavaScript must look at each property to collect keys or values, so bigger objects take more time.
Knowing how object operations scale helps you explain your code choices clearly and shows you understand how JavaScript works behind the scenes.
"What if we only needed the first key or value instead of all? How would the time complexity change?"
Practice
const obj = {a: 1, b: 2};Solution
Step 1: Understand Object.keys()
Object.keys() returns an array of the object's own enumerable property names (keys).Step 2: Compare with other methods
Object.values() returns values, Object.entries() returns key-value pairs, Object.get() is not a valid method.Final Answer:
Object.keys(obj) -> Option CQuick Check:
Keys = Object.keys(obj) [OK]
- Confusing Object.keys() with Object.values()
- Using Object.entries() to get only keys
- Trying to use Object.get() which doesn't exist
const obj = {x: 10, y: 20};?Solution
Step 1: Identify the method for values
Object.values(obj) returns an array of the object's values.Step 2: Check syntax validity
obj.values() and Object.getValues() are not valid JavaScript methods.Final Answer:
Object.values(obj) -> Option AQuick Check:
Values = Object.values(obj) [OK]
- Using Object.keys() instead of Object.values()
- Calling values() directly on the object
- Using non-existent Object.getValues() method
const obj = {a: 5, b: 10};
console.log(Object.keys(obj));Solution
Step 1: Understand Object.keys() output
Object.keys(obj) returns an array of the keys, which are 'a' and 'b'.Step 2: Check the console.log output
The console will print ['a', 'b'] as the keys array.Final Answer:
['a', 'b'] -> Option AQuick Check:
Keys = ['a', 'b'] [OK]
- Confusing keys with values
- Expecting numeric values instead of string keys
- Misreading the output as property values
const data = {name: 'Alice', age: 25};
const keys = data.keys();
console.log(keys);Solution
Step 1: Check method existence on object
Plain objects do not have a keys() method; Object.keys() is a static method.Step 2: Identify the error type
Calling data.keys() causes a TypeError: data.keys is not a function.Final Answer:
data.keys() is not a function -> Option DQuick Check:
data.keys() causes TypeError [OK]
- Calling keys() directly on object instance
- Confusing Object.keys() with instance method
- Expecting keys() to return values
const scores = {alice: 10, bob: 15, charlie: 0, dave: null};, which code snippet correctly creates an array of keys whose values are truthy?Solution
Step 1: Understand filtering keys by truthy values
We want keys where their values are truthy (non-zero, non-null).Step 2: Analyze each option
Object.keys(scores).filter(key => scores[key]) filters keys by checking if scores[key] is truthy, returning keys correctly.
Object.values(scores).filter(value => value) returns values, not keys.
Object.entries(scores).filter(([k,v]) => k).map(([k,v]) => v) filters by keys (always truthy) and maps to values, wrong.
Object.keys(scores).map(key => scores[key]) maps keys to values without filtering.Final Answer:
Object.keys(scores).filter(key => scores[key]) -> Option BQuick Check:
Filter keys by truthy values = Object.keys(scores).filter(key => scores[key]) [OK]
- Filtering values but expecting keys
- Mapping keys to values without filtering
- Filtering keys by keys instead of values
Ready to prove it?
Pass the test and this pattern moves unknown
