Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
instanceof vs typeof vs !!
(version: 0)
Comparing performance of:
instanceof vs typeof vs typeof no function vs !!
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {a:2};
Tests:
instanceof
obj instanceof Object
typeof
typeof obj === 'object'
typeof no function
typeof obj === 'object' && typeof obj !== 'function'
!!
obj.a
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
instanceof
typeof
typeof no function
!!
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
instanceof
8864094.0 Ops/sec
typeof
22400460.0 Ops/sec
typeof no function
11924350.0 Ops/sec
!!
22561318.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided JSON represents a benchmark that tests three different approaches to determine if an object is not null or undefined: 1. `instanceof` operator 2. `typeof` operator with a string comparison 3. The double exclamation mark (`!!`) operator The benchmark creates an object `obj` with a single property `a`, which has the value 2. **Options Compared** The three options being compared are: * `instanceof`: Checks if the object is an instance of the `Object` constructor. * `typeof`: Returns the type of the object as a string. The comparison `typeof obj === 'object'` checks if the object is an object, but since JavaScript objects do not have a `constructor` property, this comparison can be ambiguous. However, in most cases, it will return `'object'`. The second comparison, `typeof obj === 'object' && typeof obj !== 'function'`, is specific to objects that are not functions. * `!!`: This operator checks if the object is not null or undefined by converting it to a boolean value (truthy or falsy). In this case, since the object has a property `a` with a value of 2, it will be truthy. **Pros and Cons** Here's a brief overview of each option: * `instanceof`: This method can be more reliable than `typeof`, as it directly checks if the object is an instance of a specific constructor. However, it may not work correctly for some objects, such as those created using `Object.create()`. Additionally, this method is less straightforward to understand and may require more browser support. * `typeof`: This operator is widely supported and easy to read. It returns a string value that indicates the type of the object. The double negation (`!!`) operator can be used to convert this string to a boolean value. However, as mentioned earlier, it's not foolproof for objects without a constructor property. * `!!`: This operator is simple and straightforward but may not provide the most accurate results if the object has multiple properties or values. **Library Usage** In this benchmark, there is no explicit library usage. However, some browsers like Chrome might use internal libraries to optimize their JavaScript engine performance. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark that require a deep understanding of the language. **Other Alternatives** Some alternative approaches could be: * Using `Object.prototype.toString.call(obj)` to check if the object is an instance of the `Object` constructor. * Using a library like `fast-check` or `benchmark.js` to perform more accurate and reliable benchmarking. * Using a different testing framework, such as Jest or Mocha, which provides built-in support for benchmarks. Keep in mind that these alternatives may have different pros and cons depending on the specific use case and requirements.
Related benchmarks:
instanceof vs typeof vs fast typeof object
instanceof vs typeof for objects
instanceof vs typeof franco
instanceof vs typeof vs !!value
Comments
Confirm delete:
Do you really want to delete benchmark?