Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typeof instanceof
(version: 0)
Comparing performance of:
typeof vs instanceof
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
typeof
class asdfasdf {} class sdfgsdfg {} let a = 0 const arr = [true, 5, '', 0, new asdfasdf(), new sdfgsdfg()] for (let i = 0; i < 10000000; i++) { if (typeof arr[i % 6] === 'number') { a++ } } console.log(a)
instanceof
class dfghd {} class ddfgjdjk {} let a = 0 const arr = [true, 5, '', 0, new dfghd(), new ddfgjdjk()] for (let i = 0; i < 10000000; i++) { if (arr[i % 6] instanceof dfghd) { a++ } } console.log(a)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
typeof
instanceof
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. **What is being tested?** The test measures the performance difference between using the `typeof` operator and the `instanceof` operator in JavaScript. Specifically, it tests how fast each approach can iterate over an array of objects created with two different classes (`asdfasdf` and `dfghd`) to count the number of elements that are instances of either class. **Options compared** There are two options being compared: 1. **typeof**: This operator checks the type of a value using the `[[Class]]` property of the object's prototype chain. 2. **instanceof**: This operator checks if an object is an instance of a given constructor function. **Pros and Cons of each approach:** * **typeof**: * Pros: + Lightweight and fast + Can be used for primitive types (e.g., numbers, strings) in addition to objects * Cons: + Returns the type name as a string (e.g., "number", "object"), which may not be what you want if you're looking for more information about the object's prototype chain or constructor function. * **instanceof**: * Pros: + More informative, returning the constructor function that was used to create the object + Can handle cases where an object has multiple prototypes in its chain (which can be useful for checking if a value is "of a certain type") * Cons: + Generally slower than `typeof` because it needs to traverse the object's prototype chain **Library/Constructor functions** In the test case, two classes (`asdfasdf` and `dfghd`) are used with constructors that return objects. These classes are likely custom classes created for this benchmark. The purpose of these classes is to create objects that will be tested in the benchmark. The exact implementation of these classes is not relevant to understanding the benchmark itself, but they are an essential part of the test setup. **Special JS features or syntax** This benchmark does not use any special JavaScript features or syntax beyond what's standard in the language. It relies solely on basic object-oriented programming concepts and the built-in operators `typeof` and `instanceof`. **Other alternatives** If you wanted to compare other ways of checking if an object is "of a certain type", some alternative approaches might include: * Using a separate function or method that checks for specific properties or methods in an object's prototype chain * Using a library like Lodash, which provides functions like `isPlainObject` and `isFunction` to check for object types * Using the new `typeof` operator (introduced in ECMAScript 2022) with the `strict` option, which checks if an object is of a specific type or has a certain number of properties
Related benchmarks:
typeof vs instanceof vs null
typeof vs instanceof Function
typeof_vs_instanceof
typeof vs instanceof Function vs call
instanceof vs typeof for objects
Comments
Confirm delete:
Do you really want to delete benchmark?