Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.prototype.toString.call vs typeof (only isObject)
(version: 0)
Comparing performance of:
typeof -> object vs Object.prototype.toString.call -> object
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var types = { array: [1, 2, 3], number: 123, string: '123', map: new Map([ [1, 1], [2, 2], [3, 3] ]), set: new Set([1, 2, 3]), buffer: new ArrayBuffer([1, 2, 3]), boolean: true, arrow: () => {}, function: function() {}, object: {} } var keys = Object.keys(types)
Tests:
typeof -> object
keys.map(key => typeof types[key] === "object")
Object.prototype.toString.call -> object
keys.map(key => Object.prototype.toString.call(types[key]) === '[object Object]')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
typeof -> object
Object.prototype.toString.call -> object
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 OPR/124.0.0.0
Browser/OS:
Opera 124 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
typeof -> object
6344588.5 Ops/sec
Object.prototype.toString.call -> object
2587097.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the JavaScript microbenchmark on MeasureThat.net. **Benchmark Definition** The benchmark tests two approaches to check if an object is an instance of `Object`. The script preparation code defines an object `types` with various types, including arrays, numbers, strings, maps, sets, buffers, booleans, and functions. It then extracts the keys of this object using `Object.keys()`. **Options Compared** The benchmark compares two options: 1. **`typeof`**: This operator returns a string indicating the type of the object. To check if an object is an instance of `Object`, you can use `typeof types[key] === "object"`. 2. **`Object.prototype.toString.call`**: This method returns a string representation of the object, prefixed with "[object Object]" for objects. **Pros and Cons** * **`typeof`**: * Pros: Fast and lightweight. * Cons: Can be affected by type coercion (e.g., `null` or `undefined` would return `"object"`), and may not work as expected in some cases (e.g., if the object is a primitive value). * **`Object.prototype.toString.call`**: * Pros: More accurate than `typeof`, as it returns a specific string representation for objects, regardless of type coercion. * Cons: Slower and heavier than `typeof`. **Library and Purpose** In this benchmark, the `Map` and `Set` constructors are used to create instances of these data structures. The `ArrayBuffer` constructor is used to create a buffer object. **Special JS Feature or Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's standard in modern JavaScript. **Other Alternatives** If you want to check if an object is an instance of `Object`, there are other alternatives: * **`instanceof`**: This operator checks if an object is an instance of a given constructor. You can use it like this: `types[key] instanceof Object`. * **`Object.create(null)`**: This method creates a new object with no prototype chain, and you can check if it's an instance of `Object` by verifying that its constructor is `Object`. Keep in mind that these alternatives might have different performance characteristics or behavior depending on the specific use case. In summary, the benchmark tests two common approaches to checking if an object is an instance of `Object`: using `typeof` and `Object.prototype.toString.call`. The choice between these options depends on your priorities: speed vs. accuracy and robustness.
Related benchmarks:
Object vs toString
Testing123
isarray_vs_instanceof_vs_tostring
Array.from().map vs Array.prototype.map.call
isString vs
Comments
Confirm delete:
Do you really want to delete benchmark?