Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Instanceof vs compare constructor
(version: 1)
Comparing performance of:
instanceof vs ctor
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var collectionSet = new Set([Map, Set, WeakMap, WeakSet]) var a = { a: 1 } var b = {} var c = new Map() class SomeMap extends Map {} class SomeSet extends Set {} var d = new SomeMap() var e = new SomeSet() var cases = [a, b, c, d, e] var index = ~(Math.random() * 10 % 5) + 5
Tests:
instanceof
console.log([...collectionSet].some(v=>cases[index] instanceof v))
ctor
console.log(collectionSet.has(cases[index].construtor))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
instanceof
ctor
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):
I'll break down the benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark definition is a JSON object that describes the test scenario. It consists of: * `Name`: The name of the benchmark. * `Description`: A brief description of the benchmark (null in this case). * `Script Preparation Code`: A JavaScript code snippet that sets up the environment for the benchmark. This code creates several variables and objects, including: + `collectionSet`: A Set containing various types of maps and sets (`Map`, `Set`, `WeakMap`, and `WeakSet`). + `a`, `b`, and `c`: Three objects with different properties (e.g., `a` has a property `a`, while `b` is empty, and `c` is an instance of `Map`). + `d` and `e`: Two instances of custom classes `SomeMap` and `SomeSet`, respectively. + `cases`: An array containing all the objects created above. * `Html Preparation Code`: An empty string (null), indicating that no HTML code needs to be prepared for this benchmark. **Individual Test Cases** The benchmark consists of two test cases: 1. **"instanceof"**: This test case uses the `some()` method to check if each element in `collectionSet` is an instance of a specific value from the `cases` array. 2. **"ctor"**: This test case checks if the constructor property (`construtor`) of each object in `cases` is present in `collectionSet`. **Libraries and Special Features** * The custom classes `SomeMap` and `SomeSet` are likely used to create instances that can be compared with the built-in types (e.g., `Map`, `Set`). These classes might not provide any additional functionality beyond what's offered by their parent classes. * No special JavaScript features or syntax are mentioned in the benchmark definition. **Pros and Cons of Different Approaches** 1. **Using instanceof vs checking constructor property**: Both approaches have pros and cons: + Using `instanceof`: - Pros: More readable, concise code; allows for easy addition of new cases. - Cons: Can be slower due to the use of the `some()` method, which iterates over the elements in the set. + Checking constructor property (`construtor`): - Pros: Faster, since it directly accesses a single property on each object; doesn't require iteration over the set. - Cons: Less readable and less concise code; requires explicit checking of each case. 2. **Using a Set vs an Array**: Using a `Set` provides faster lookups compared to using an `Array`, since sets use a hash table for storage, allowing for O(1) lookup time. **Other Alternatives** * Another approach could be to use a library like Lodash's `includes()` function, which is designed specifically for checking membership in an array or set. However, this would likely introduce additional dependencies and overhead. * Alternatively, the benchmark could focus on measuring the performance of specific JavaScript features or syntax, such as async/await or generators. Keep in mind that these alternatives are not directly related to the original question, but they might be worth exploring if you're looking for ways to optimize your own code.
Related benchmarks:
typeof vs instanceof vs null
instanceof vs typeof for objects
Check object. typeof vs constructor + null check
Check object. typeof vs constructor
JS instanceof vs in
Comments
Confirm delete:
Do you really want to delete benchmark?