var f = () => {};
f instanceof Function
typeof f === 'function'
f.call
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
instanceof | |
typeof | |
call |
Test name | Executions per second |
---|---|
instanceof | 139461760.0 Ops/sec |
typeof | 170190432.0 Ops/sec |
call | 156926304.0 Ops/sec |
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:
f instanceof Function
: This checks if the object f
is an instance of the Function
constructor.typeof f === 'function'
: This checks if the type of the variable f
is 'function'
, which is a string literal.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:
f instanceof Function
:typeof f === 'function'
:f
is a function expression (not a declaration) or if it's assigned to a variable that shadows the global Function
object.f.call()
: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:
var f = () => {};
syntax creates a function expression.typeof
operator is used to check the type of f
.instanceof
operator is used to check if f
is an instance of the Function
constructor.Other Considerations
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:
isFunction()
or Moment.js's isFunction()
.Keep in mind that the results of this benchmark may not be representative for all environments or use cases.