Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
instanceof vs regular comparison
(version: 2)
We try to check if comparing value using regular existence comparison is more efficient than comparing using instanceof.
Comparing performance of:
Check with regular comparison vs Check using instanceof comparison vs Check using regular and instanceof comparison
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var dateList = []; var currentDateTime = Date.now(); for (i = 0; i < 10000; i++) { var randomInt = Math.round(Math.random()); // This will randomly returns either 0 or 1. if (randomInt === 0) { dateList.push(null); // Fill the array with a null value. } else { dateList.push(addMinutes(currentDateTime, i)); // This will fill up the array with date objects. } } function addMinutes(dt, minutes) { return new Date(dt + minutes * 60000); }
Tests:
Check with regular comparison
var counter = 0; for (i = 0; i < 10000; i++) { if (!!dateList[i]) { counter++; } }
Check using instanceof comparison
var counter = 0; for (i = 0; i < 10000; i++) { if (dateList[i] instanceof Date) { counter++; } }
Check using regular and instanceof comparison
var counter = 0; for (i = 0; i < 10000; i++) { if (!!dateList[i] && dateList[i] instanceof Date) { counter++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Check with regular comparison
Check using instanceof comparison
Check using regular and instanceof comparison
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 benchmarking test cases. **Benchmark Definition** The benchmark measures the efficiency of two different approaches to check if a value exists or is an instance of a specific type: 1. **Regular comparison**: Using `!!dateList[i]` to check if `i` is within the bounds of the `dateList` array. 2. **`instanceof` comparison**: Using `dateList[i] instanceof Date` to check if the value at index `i` in the `dateList` array is an instance of the `Date` type. **Options being compared** The two options being compared are: * Regular comparison (`!!dateList[i]`) * `instanceof` comparison (`dateList[i] instanceof Date`) **Pros and Cons** * **Regular comparison**: + Pros: Simple, easy to understand, and widely supported by most browsers. + Cons: Can be slower for large arrays, as it involves a loop and checks each element individually. * `instanceof` comparison: + Pros: Faster than regular comparison for large arrays, as it uses the optimized internal logic of the browser's JavaScript engine. + Cons: Less intuitive and less widely supported by older browsers. **Library usage** The benchmark uses the `Date` type from the JavaScript Standard Library. The `addMinutes` function is a custom utility function that returns a new `Date` object created by adding minutes to a given date. **Special JS features or syntax** There are no special JavaScript features or syntax used in this benchmark, except for the use of the spread operator (`...`) which is not necessary and can be omitted. The benchmark focuses on comparing two basic approaches to existence checking. **Other alternatives** If you want to compare performance with other methods, you could consider using: * **WeakMap`**: A built-in JavaScript object that provides fast membership testing. * **Array.prototype.includes()`**: A method that checks if a value exists in an array using the optimized internal logic of the browser's JavaScript engine. Keep in mind that the best approach depends on your specific use case and requirements. For existence checking, `instanceof` comparison might be a good choice for large arrays, while regular comparison might be sufficient for small arrays or when readability is more important than performance.
Related benchmarks:
new Date().getTime() vs Date.now()
Date.now() vs new Date().getTime() vs + new Date
Date.now() - Date.now() vs new Date() - new Date()
Crypto.randomUUID() vs new Date().getTime()
Comments
Confirm delete:
Do you really want to delete benchmark?