Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typeof vs instanceof vs constructor vs toString
(version: 0)
Comparing performance of:
typeof vs instanceof vs constructor vs toString
Created:
4 years ago
by:
Registered User
Go to the latest result
Script Preparation code:
var data = 1000n;
Tests:
typeof
console.log(typeof(data) == 'bigint');
instanceof
console.log(data instanceof BigInt);
constructor
console.log(data.constructor == BigInt);
toString
console.log(Object.prototype.toString.call(data) == '[object BigInt]');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
typeof
instanceof
constructor
toString
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
typeof
198K/s
instanceof
194K/s
constructor
193K/s
toString
191K/s
View exact numbers
Test name
Executions per second
✓
typeof
198,355 Ops/sec
instanceof
193,881 Ops/sec
constructor
193,294 Ops/sec
toString
190,925 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided benchmark measures the performance of different ways to check if a value is a `BigInt` in JavaScript. A `BigInt` (Big Integer) is a type in JavaScript that can handle arbitrarily large integers, which are useful for mathematical operations and cryptographic applications. Let's analyze each test case: 1. **typeof**: This checks if the result of `typeof data` equals `'bigint'`. The problem with this approach is that it may return incorrect results when the value is not a `BigInt`, but rather an integer in the range of 53 bits (a limitation of JavaScript integers). Additionally, some JavaScript engines might optimize away this check. 2. **instanceof**: This checks if `data` is an instance of `BigInt`. While more accurate than `typeof`, this method still has limitations: `BigInt` instances are not iterable or enumerable in the same way as other objects. However, since a single value can't be both an integer and a BigInt at the same time (except for the specific number 0n), this will only return true if you have created a number object with the BigInt type. 3. **constructor**: This checks if the constructor of `data` is `BigInt`. This method is more accurate than `typeof`, but it also has limitations: since `BigInt` instances do not have constructors (i.e., they are not objects), this will return false for all cases where data would be a BigInt. 4. **toString**: This checks if the result of calling `Object.prototype.toString.call(data)` equals `'[object BigInt]'`. While more accurate than `typeof`, this method has limitations since it requires an object reference (the function is called on `data`). Considering these points, the order of performance from best to worst might be: - instanceof - typeof - toString - constructor **Pros and Cons:** * instanceof: Pros - more accurate, Cons - not directly supported in all JavaScript environments, because it is not defined for BigInt. * typeof: Pros - Simple, but the limitation on 53 bit integers makes this less reliable than instanceof. It also returns false positives in certain scenarios (e.g., when you're comparing a string with the result of typeof). * toString: Pros - Works on all platforms and browsers where the first two methods don't. However, calling Object.prototype.toString.call() is an additional operation which can slow down the test. * constructor: The least reliable option for testing, because no BigInt object has a constructor function. **Other Considerations:** If you want to make this benchmark more comprehensive, consider adding tests for: - Checking if data is of type `number`, which would return true positives in cases where data has 53-bit or lower precision. - Checking if data is an instance of another class.
Related benchmarks
instanceof vs typeof vs fast typeof object
typeof vs instanceof Function vs call
Check object. typeof vs constructor
Comments
Confirm delete:
Do you really want to delete benchmark?