Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typeof function vs call in function
(version: 0)
Comparing performance of:
typeof function vs call in function vs !!fn?.call vs fn?.call != null vs fn?.call != void 0 vs isFuncTrue = fn?.call != undefined;
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var fn = function(){}; var obj = {}; var objNull = null; var unknown; // undefined
Tests:
typeof function
var isFuncTrue = typeof fn === 'function'; var isFuncFalse = typeof obj === 'function'; var isFuncFalse2 = typeof objNull === 'function'; var isFuncFalse3 = typeof unknown === 'function';
call in function
var isFuncTrue = fn && 'call' in fn; var isFuncFalse = obj && 'call' in obj; var isFuncFalse2 = objNull && 'call' in objNull; var isFuncFalse3 = unknown && 'call' in unknown;
!!fn?.call
var isFuncTrue = !!fn?.call; var isFuncFalse = !!obj?.call; var isFuncFalse2 = !!objNull?.call; var isFuncFalse3 = !!unknown?.call;
fn?.call != null
var isFuncTrue = fn?.call != null; var isFuncFalse = obj?.call != null; var isFuncFalse2 = objNull?.call != null; var isFuncFalse3 = unknown?.call != null;
fn?.call != void 0
var isFuncTrue = fn?.call != void 0; var isFuncFalse = obj?.call != void 0; var isFuncFalse2 = objNull?.call != void 0; var isFuncFalse3 = unknown?.call != void 0;
isFuncTrue = fn?.call != undefined;
var isFuncTrue = fn?.call != undefined; var isFuncFalse = obj?.call != undefined; var isFuncFalse2 = objNull?.call != undefined; var isFuncFalse3 = unknown?.call != undefined;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
typeof function
call in function
!!fn?.call
fn?.call != null
fn?.call != void 0
isFuncTrue = fn?.call != undefined;
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):
**What is being tested?** The provided benchmark tests the performance of different ways to check if a value is a function in JavaScript. The test cases compare the following approaches: 1. Using `typeof` operator (`typeof fn === 'function'`) 2. Checking if an object has a `call` property using the `in` operator (`'call' in obj`) 3. Using optional chaining (`!!fn?.call`) 4. Checking if the result of calling a value with `()` is not null or undefined (`fn?.call != null`, `fn?.call != void 0`, and `fn?.call != undefined`) **Options comparison** Each test case compares two approaches: * The first approach is the one being tested. * The second approach is a common alternative. For example, in the "typeof function" test case, the first approach uses `typeof fn === 'function'`, while the second approach uses `'call' in fn`. **Pros and cons of each approach** 1. **`typeof` operator**: This approach is simple and widely supported. However, it can be slower than other approaches because it involves a string comparison. 2. **Checking if an object has a `call` property using `in` operator**: This approach is fast because it only checks if the property exists, but it may not work for all types of values (e.g., strings). 3. **Optional chaining (`!!fn?.call`)**: This approach is concise and modern, but it requires support for optional chaining in older browsers. 4. **Checking if the result of calling a value with `()` is not null or undefined**: This approach is simple and can be faster than using `typeof` or checking for `call` property, but it may not work for all types of values (e.g., objects without `call` method). **Library usage** None of the test cases use any external libraries. **Special JavaScript features or syntax** The benchmark uses optional chaining (`!!fn?.call`) and the `in` operator to check if an object has a `call` property. These are modern JavaScript features introduced in ECMAScript 2018 (ES2018). **Other alternatives** Other approaches that could be used to test this benchmark include: * Using a function constructor (`new Function()`): This approach is slower than checking for the existence of a `call` property. * Using a closure: This approach is not relevant to this specific benchmark. Note that these alternative approaches may not be as efficient or concise as the ones being tested, and may not work in all cases.
Related benchmarks:
Check function. typeof vs constructor + null check
typeof vs instanceof Function
Check function. typeof vs constructor + null check II
typeof vs instanceof Function vs call
instanceof vs typeof function
Comments
Confirm delete:
Do you really want to delete benchmark?