Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typeof vs instanceof Function vs call
(version: 0)
Comparing performance of:
instanceof vs typeof vs call
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var f = () => {};
Tests:
instanceof
f instanceof Function
typeof
typeof f === 'function'
call
f.call
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
instanceof
typeof
call
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
instanceof
139461760.0 Ops/sec
typeof
170190432.0 Ops/sec
call
156926304.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is tested, compared options, pros and cons, and other considerations. **Benchmark Definition** The benchmark definition represents three different ways to check if a value is a function: 1. `f instanceof Function` : This checks if the object `f` is an instance of the `Function` constructor. 2. `typeof f === 'function'`: This checks if the type of the variable `f` is `'function'`, which is a string literal. 3. `f.call()`: This calls the function immediately and passes no arguments, returning the result. **Options Compared** The three options are compared in terms of their performance. The benchmark aims to determine which method is the fastest. **Pros and Cons of Each Approach:** 1. **`f instanceof Function`**: * Pros: Simple and straightforward way to check if a value is a function. * Cons: May be slower than other methods due to the overhead of constructor checking. 2. **`typeof f === 'function'`**: * Pros: Fast and efficient, as it relies on type checking instead of constructor checking. * Cons: May not work correctly if `f` is a function expression (not a declaration) or if it's assigned to a variable that shadows the global `Function` object. 3. **`f.call()`**: * Pros: Can be fast and efficient, as it simply calls the function without checking its type or constructor. * Cons: May not work correctly if `f` is not a function (e.g., null, undefined), and may throw errors if the function requires arguments. **Library and Special JavaScript Features** There are no libraries used in this benchmark. However, some JavaScript features are implicitly used: 1. **Function expressions**: The `var f = () => {};` syntax creates a function expression. 2. **Type checking**: The `typeof` operator is used to check the type of `f`. 3. **Constructor checking**: The `instanceof` operator is used to check if `f` is an instance of the `Function` constructor. **Other Considerations** 1. **Browser differences**: The benchmark results show different numbers for each test case, indicating that browser differences may affect performance. 2. **Device platform and operating system**: The results are specific to a desktop Windows 10 environment, which may not reflect performance in other environments. 3. **Execution frequency**: The `ExecutionsPerSecond` values indicate the number of executions per second for each test case. **Alternatives** If you need to compare different approaches to checking if a value is a function, consider using this benchmark as a starting point and exploring alternative methods, such as: 1. Using a library like Lodash's `isFunction()` or Moment.js's `isFunction()`. 2. Implementing a custom function that uses a combination of constructor checking and type checking. 3. Testing with different types of functions (e.g., arrow functions, constructor functions) to evaluate performance in various scenarios. Keep in mind that the results of this benchmark may not be representative for all environments or use cases.
Related benchmarks:
Function: typeof vs instanceof
typeof vs instanceof vs null
typeof vs instanceof Function
instanceof vs typeof function
Comments
Confirm delete:
Do you really want to delete benchmark?