Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Call or check
(version: 0)
Comparing performance of:
Check es5, not call vs Check es6, not call vs No check, call
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var EMPTY_FN = () => {}; var props = {};
Tests:
Check es5, not call
const { onValidated } = props; if (typeof onValidated === 'function') onValidated();
Check es6, not call
const { onValidated } = props; onValidated?.();
No check, call
const { onValidated = EMPTY_FN } = props; onValidated();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Check es5, not call
Check es6, not call
No check, call
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0
Browser/OS:
Chrome 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Check es5, not call
90057464.0 Ops/sec
Check es6, not call
86406592.0 Ops/sec
No check, call
65211388.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is tested?** The provided JSON represents a benchmark for measuring the performance of different approaches in calling or checking a function in JavaScript. The test cases compare three scenarios: 1. **Check (ES5)**: Verifies if `onValidated` is a function using the `typeof` operator and then calls it. 2. **Check (ES6)**: Similar to the previous one, but uses the optional chaining operator (`?.`) to access the `onValidated` property without explicitly checking its type. 3. **No check, call**: Calls `onValidated` directly without any checks. **Options compared** The three options are: * **Check (ES5)**: Verifies if `onValidated` is a function using `typeof`. * **Check (ES6)**: Uses the optional chaining operator (`?.`) to access `onValidated` without explicit type checking. * **No check, call**: Directly calls `onValidated`. **Pros and Cons** Here's a brief overview of each approach: * **Check (ES5)**: * Pros: * Simple and well-supported by older browsers. * Easy to understand for developers familiar with the `typeof` operator. * Cons: * May lead to slower performance due to unnecessary type checks. * Not as efficient as other approaches. * **Check (ES6)**: * Pros: * More concise and expressive than explicit type checking using `typeof`. * Faster performance since it avoids unnecessary checks. * Cons: * May not be supported by older browsers that don't have ES6 enabled. * Requires knowledge of the optional chaining operator (`?.`). * **No check, call**: * Pros: * Fastest performance since no checks are performed before calling `onValidated`. * Suitable for situations where performance is critical and type checking isn't necessary. * Cons: * May lead to errors if `onValidated` is not a function. * Requires careful consideration of potential edge cases. **Library** The provided JSON uses the `props` object, which appears to be an object containing variables or properties that can be used in the benchmark script. The purpose of `props` is to provide a way to encapsulate the test data and behavior within the benchmark definition. **Special JS feature/syntax** The benchmark makes use of optional chaining (`?.`) introduced in ES6, which allows accessing a property or calling a method without explicit type checking. This syntax is supported by most modern browsers but may not be available in older ones. Now that we've covered the details, let's discuss alternatives to these approaches: * **Using `in` operator**: Instead of using `typeof`, you can check if `onValidated` is a property of the `props` object using the `in` operator. ```javascript if (onValidated in props) { onValidated(); } ``` * **Using a function to check type**: You can create a separate function that checks the type of `onValidated` and then calls it if it's valid. ```javascript function checkType(onValidated) { if (typeof onValidated === 'function') { onValidated(); } } checkType(props.onValidated); ``` * **Using a library or framework**: Depending on your use case, you might consider using a JavaScript library or framework that provides a way to perform type checks and calls in a more concise and expressive manner. Keep in mind that the choice of approach ultimately depends on your specific requirements, performance constraints, and target audience.
Related benchmarks:
Check if obj.prop is undefined
Checking property existence
Delete undefined property
lodash omit vs native function
Comments
Confirm delete:
Do you really want to delete benchmark?