Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
isfn vs switch
(version: 0)
Comparing performance of:
isCollection1 vs isCollection2
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var isMap = (val) => val?.constructor?.name === 'Map' var isSet = (val) => val?.constructor?.name === 'Set' var isWeakMap = (val) => val?.constructor?.name === 'WeakMap' var isWeakSet = (val) => val?.constructor?.name === 'WeakSet' var isCollection1 = (val) => isMap(val) || isSet(val) || isWeakMap(val) || isWeakSet(val) var isCollection2 = (val) => { switch(val?.constructor?.name) { case 'Map': case 'Set': case 'WeakMap': case 'WeakSet': return true default: return false } } console.log(isCollection1(new Set()), isCollection1(123)) console.log(isCollection2(new Set()), isCollection2(123))
Tests:
isCollection1
var a = isCollection1(new Set()) var b = isCollection1(123)
isCollection2
var a = isCollection2(new Set()) var b = isCollection2(123)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
isCollection1
isCollection2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
isCollection1
2756192.0 Ops/sec
isCollection2
7008870.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark defines two different approaches to check if a value is a collection (e.g., `Set`, `Map`, `WeakSet`, or `WeakMap`). There are two scripts: 1. **`isCollection1`**: This script uses an explicit function declaration with an arrow function (`=>`) and checks the constructor name of the value using the optional chaining operator (`?.`). It returns `true` if any of these collection types match. 2. **`isCollection2`**: This script uses a traditional `switch` statement to check the constructor name of the value. **Options Compared** The two scripts are compared in terms of performance, which is represented by the `ExecutionsPerSecond` metric in the benchmark results. **Pros and Cons** 1. **`isCollection1`**: * Pros: + More concise and readable code + Avoids the overhead of a traditional `switch` statement + Uses optional chaining, which is a modern JavaScript feature that can improve performance * Cons: + May not be supported in older browsers or environments + Can be slower due to the use of optional chaining (`?.`) 2. **`isCollection2`**: * Pros: + More readable code, especially for those familiar with traditional `switch` statements + Should work in all browsers and environments that support JavaScript 1.0 (the minimum version required by most modern browsers) * Cons: + Longer and more verbose code compared to `isCollection1` + More overhead due to the traditional `switch` statement **Library and Purpose** There is no explicit library mentioned in the benchmark definition, but it uses some standard JavaScript features: * Optional chaining (`?.`) is used to safely access properties of an object without throwing a runtime error if they don't exist. * The `Map`, `Set`, `WeakMap`, and `WeakSet` constructors are part of the built-in JavaScript API for working with collections. **Special JS Feature or Syntax** The benchmark uses optional chaining (`?.`) which is a modern JavaScript feature that was introduced in ECMAScript 2020. It allows you to access properties of an object without throwing a runtime error if they don't exist, using the nullish coalescing operator (`??`). This syntax is not supported in older browsers or environments. **Other Considerations** When choosing between these two approaches, consider the trade-offs: * If performance is critical and you're targeting modern browsers that support JavaScript 2020 features, `isCollection1` might be a better choice. * If code readability and maintainability are more important than performance, `isCollection2` might be a better choice. Keep in mind that this benchmark only tests the specific use case of checking if a value is a collection. In general, you should consider other factors when choosing between these approaches, such as the specific requirements of your project and the target audience's browser compatibility.
Related benchmarks:
Map has vs get
Create vs Mutate Object 2
JavaScript spread operator vs Object.assign vs mutation performance with condition #7
Comparing and Filtering two arrays, using the set.has vs. array.includes vs array.indexOf
Switch/case vs indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?