Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Plain object check: Object.prototype.toString vs Object.getPrototypeOf
(version: 0)
Comparing performance of:
Object.prototype.toString vs Object.getPrototypeOf
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var PLAIN_OBJECT_PROTO = Object.getPrototypeOf({}) var obj = { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1 };
Tests:
Object.prototype.toString
for (let i=10000; i > 0; i--) { console.log(Object.prototype.toString.call(obj) === '[object Object]' ? 'P' : 'F'); }
Object.getPrototypeOf
for (let i=10000; i > 0; i--) { console.log(obj !== null && typeof obj === 'object' && Object.getPrototypeOf(obj) === PLAIN_OBJECT_PROTO ? 'P' : 'F') }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.prototype.toString
Object.getPrototypeOf
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/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.prototype.toString
16.3 Ops/sec
Object.getPrototypeOf
14.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Overview** The benchmark measures the performance difference between two approaches to check if an object is an instance of `Object`. The two approaches are: 1. Using `Object.prototype.toString()` 2. Using `Object.getPrototypeOf()` in conjunction with `typeof` and a null check **Options Compared** The benchmark compares the execution speed of the two approaches. Pros and Cons: * **Approach 1: `Object.prototype.toString()`** + Pros: - Simple and concise - Fast, as it's a built-in method that directly checks if an object is an instance of `Object` + Cons: - May not work correctly for certain types of objects (e.g., arrays, functions) - Can be slower due to the overhead of calling a method on the prototype chain * **Approach 2: `Object.getPrototypeOf()` + `typeof` + null check** + Pros: - More robust and accurate, as it checks for multiple conditions (being an object, having a prototype) - May be faster due to avoiding the overhead of calling `toString()` + Cons: - Longer and more complex code - May have additional overhead due to the use of `getPrototypeOf()` and `typeof` **Library and Special JS Feature** * **`Object.getPrototypeOf()`**: This method returns the prototype object associated with a given object. It's a part of the ECMAScript standard since ES5. * **`typeof`**: This operator checks the type of an object, returning one of several strings (e.g., "object", "number", etc.). While not specific to JavaScript, `typeof` is widely supported and commonly used in JavaScript development. **Other Considerations** The benchmark assumes that the test objects are plain objects with multiple properties. If the test objects were more complex (e.g., arrays or functions), either approach might be affected by additional overhead. It's worth noting that the choice of approach may depend on specific use cases and requirements. For example, if you need to check if an object is a concrete instance of `Object`, Approach 1 might be sufficient. However, if you want to ensure that an object has a prototype chain, Approach 2 is likely more suitable. **Alternatives** Other alternatives for checking if an object is an instance of `Object` include: * Using the `instanceof` operator (introduced in ECMAScript 5) * Using a library like Lodash's `isPlainObject()` function However, these alternatives may not be as widely supported or optimized as the built-in methods and approach combinations used in the benchmark.
Related benchmarks:
Object vs toString
Check object. typeof vs constructor
isString vs
Instanceof VS toString for date comparison when using objects
Comments
Confirm delete:
Do you really want to delete benchmark?