Write a function `isEmpty(obj)` that returns true if the object has no own enumerable properties.
Problem Statement
Examples
Input: obj = {}
Output: true
Explanation: Object has no own enumerable properties.
Input: obj = { a: 1 }
Output: false
Explanation: Object contains key "a".
Complexity
Time Complexity: O(N)
Space Complexity: O(1)
Hints
Editorial & Approach
Problem Overview & Intuition
To solve Check if Object is Empty, we consider the execution characteristics of JavaScript engines. Write a function `isEmpty(obj)` that returns true if the object has no own enumerable properties. By utilizing idiomatic language constructs and clean algorithmic principles, we can accomplish this with optimal time and memory usage.
Step-by-Step Approach
- Understand Problem Contract: Identify input arguments, return type expectations, and edge cases (empty inputs, nullish values).
- Choose Core Mechanism: Use modern JavaScript patterns (object.keys() returns an array of an object's own property names).
- Implement Logic: Handle state and transformations efficiently (an empty object will have object.keys().length === 0).
- Return Result: Ensure proper return format and preserve caller context if applicable.
Optimal Implementation (JavaScript)
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
Complexity Analysis
Edge Cases & Corner Traps Handled
- Empty or boundary inputs (empty arrays, strings, zero length).
- Type checks and unexpected values (e.g.
null,undefined, negative numbers). - Closure preservation and memory isolation between separate invocations.