Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Function: typeof vs instanceof
(version: 0)
Comparing performance of:
instanceof vs typeof
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const f = () => {}; let i = 0;
Tests:
instanceof
if (f instanceof Function) i++;
typeof
if (typeof f === 'function') i++
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
instanceof
typeof
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
instanceof
36468924.0 Ops/sec
typeof
37488652.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, the pros and cons of each approach, and other considerations. **Benchmark Overview** The benchmark is designed to compare two approaches for checking if an object is a function in JavaScript: `instanceof` and `typeof`. **Test Cases** There are two test cases: 1. **`instanceof`**: The benchmark checks if the value of `f instanceof Function` is truthy. 2. **`typeof`**: The benchmark checks if the result of `typeof f` is `'function'`. **Options Compared** The two options being compared are: * Using `instanceof` to check if an object is a function * Using `typeof` with the `=== 'function'` comparison operator to check if an object is a function **Pros and Cons of Each Approach** 1. **`instanceof`**: * Pros: + More precise, as it checks if the prototype chain of the object includes Function. + Can be useful in certain situations where `typeof` might return `'object'` instead of `'function'`. * Cons: + May be slower due to the overhead of checking the prototype chain. 2. **`typeof`**: * Pros: + Faster, as it only checks the type of the object without evaluating its prototype chain. + Often used in everyday JavaScript development. * Cons: + Might return `'object'` instead of `'function'`, which could lead to incorrect results. **Other Considerations** * **Library Usage**: Neither test case uses any external libraries, so it's a bare-metal comparison of the two approaches. * **JavaScript Features**: The benchmark does not use any special JavaScript features or syntax that might affect the results. **Alternative Approaches** If you were to add more test cases, you could consider other methods for checking if an object is a function, such as: * Using `Function()` constructor: `new Function() === f` * Using `Object.getPrototypeOf()`: `Object.getPrototypeOf(f) === Function.prototype` Keep in mind that these alternative approaches might have different trade-offs in terms of performance and precision. In summary, the benchmark compares two common methods for checking if an object is a function in JavaScript: `instanceof` and `typeof`. While both have their pros and cons, `typeof` is generally faster but may return incorrect results in certain situations.
Related benchmarks:
typeof vs instanceof Function
Check function. typeof vs constructor + null check II
(instanceof Function) vs (typeof function)
typeof vs instanceof Function vs call
Comments
Confirm delete:
Do you really want to delete benchmark?