Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Is plain object
(version: 0)
Comparing performance of:
isPlainObject vs typeof and null check
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function isObject(o) { return Object.prototype.toString.call(o) === '[object Object]'; } function isPlainObject(o) { var ctor,prot; if (isObject(o) === false) return false; // If has modified constructor ctor = o.constructor; if (ctor === undefined) return true; // If has modified prototype prot = ctor.prototype; if (isObject(prot) === false) return false; // If constructor does not have an Object-specific method if (prot.hasOwnProperty('isPrototypeOf') === false) { return false; } // Most likely a plain Object return true; };
Tests:
isPlainObject
const a = {a:1} isPlainObject(a)
typeof and null check
const a = {a:1} a !== null && typeof a === 'object'
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
isPlainObject
typeof and null check
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its options. **Benchmark Definition** The test case is defined by two functions: `isObject` and `isPlainObject`. The main function, `isPlainObject`, checks if an object is a plain object (i.e., not a prototype or derived from another constructor). Here's what it does: 1. Calls the `isObject` function to check if the object is of type `Object`. 2. If true, checks if the object has modified its constructor. 3. If no modification, checks if the object's prototype has an `isPrototypeOf` method (a property specific to Objects). 4. If this property exists, it's likely that the object is a plain Object. **Options compared** The two options being tested are: 1. **`isObject(o) === false`: This option simply checks if the object is not of type `Object`. However, this approach has some drawbacks: * It doesn't account for derived constructors (e.g., `Array`). * It's a simple equality check that can be optimized away by modern browsers. 2. **`isPlainObject(o)`: This option checks for the presence of the `isPrototypeOf` method on the object's prototype, which is more robust and accurate. **Pros and Cons** 1. **`isObject(o) === false`**: * Pros: Simple, easy to implement, and can be optimized away by modern browsers. * Cons: Doesn't account for derived constructors, leading to incorrect results in those cases. 2. **`isPlainObject(o)`**: * Pros: More accurate, takes into account derived constructors, and provides a more reliable result. * Cons: Slightly slower due to the additional checks. **Library** In the provided code, `isObject` is not explicitly defined as a library, but rather as an anonymous function. However, in general, `isObject` could be considered a utility function for checking if an object is of type `Object`. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. **Alternatives** Other alternatives to implement the `isPlainObject` check could include: 1. Using a library like `lodash.isPlainObject` (if you're using Lodash). 2. Implementing a custom function that checks for the presence of the `isPrototypeOf` method on the object's prototype. 3. Using a more modern approach, such as checking if an object has the `constructor` property and then verifying its type. Keep in mind that this benchmark focuses on testing the specific implementation of `isPlainObject`, so alternatives might not be directly comparable.
Related benchmarks:
testtest132123asdasda
Object.prototype.hasOwnProperty vs obj.hasOwnProperty vs exists check
Test checking of literal object types
Test checking of literal object types - v2
Comments
Confirm delete:
Do you really want to delete benchmark?