Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Discriminated Union vs InstanceOf
(version: 0)
Comparing performance of:
Classes vs Union
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function Shape() {} function Circle() {} Circle.prototype = new Shape(); function Square() {} Square.prototype = new Shape() function Rectangle () {} Rectangle.prototype = new Square(); function circle() { return { kind: 0 }; } function isCircle(t) { return t.kind === 0; } function square() { return { kind: 1 } } function isSquare(t) { return t.kind === 1; } function rectangle() { return { kind: 2 } } function isRectangle(t) { return t.kind === 2; }
Tests:
Classes
const c = new Circle(); const s = new Square(); const r = new Rectangle(); console.log(c instanceof Circle); console.log(c instanceof Square); console.log(s instanceof Circle); console.log(s instanceof Square); console.log(r instanceof Rectangle);
Union
const c = circle(); const s = square(); const r = rectangle(); console.log(isCircle(c)); console.log(isSquare(c)); console.log(isCircle(s)); console.log(isSquare(s)); console.log(isRectangle(r));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Classes
Union
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 on the provided JSON?** The benchmark tests two approaches for discriminated unions in JavaScript: 1. **Classes**: This approach uses classes to define the shapes (Circle, Square, Rectangle) and their instances. It then checks if each instance is an instance of its corresponding shape using the `instanceof` operator. 2. **Union**: This approach uses functions (circle(), square(), rectangle()) that return objects with a specific kind property, which is used to identify the type of object. **Options being compared** The benchmark compares the performance of these two approaches: 1. Classes 2. Union **Pros and Cons of each approach:** * **Classes**: Pros: + More intuitive and expressive way to define shapes and their instances. + Can be more efficient for complex types with multiple inheritance paths. + Can be used in conjunction with other JavaScript features like type annotations (e.g., TypeScript). * Cons: + May incur overhead due to class creation and prototype chaining. + Can be less flexible than function-based approaches, as each shape must be defined separately. * **Union**: Pros: + More concise and lightweight way to define shapes and their instances. + Can be more efficient for simple types with few inheritance paths. + Does not incur overhead due to class creation or prototype chaining. * Cons: + May require more boilerplate code to define the functions and objects. + Less intuitive than class-based approaches, as the type is inferred from the object's properties. **Special JS feature:** The benchmark uses a special JavaScript feature called "discriminated unions," which allows multiple types to be defined in a single syntax. This feature is supported by modern JavaScript engines, including those used on mobile devices. **Library usage** None of the provided test cases use any external libraries. **Other alternatives** If you're looking for alternative approaches to discriminated unions or want to explore other options, consider the following: 1. **Prototypal inheritance**: Instead of using classes or functions, you can define shapes as objects with specific prototype chains. 2. **Type annotations (e.g., TypeScript)**: You can use type annotations to specify the types of variables and function return values, which can help catch errors at compile-time rather than runtime. 3. **Type guards**: If you're using a functional programming style, you can define type guards as separate functions that check the type of an object and return a boolean indicating its validity. In summary, the benchmark provides a comparison between two approaches to discriminated unions in JavaScript: classes vs union. The choice of approach depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
Function: typeof vs instanceof
Check function. typeof vs constructor + null check
Check function. typeof vs constructor + null check II
(instanceof Function) vs (typeof function)
typeof vs instanceof Function vs call
Comments
Confirm delete:
Do you really want to delete benchmark?