Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
cost of method as symbol vs normal class method
(version: 1)
Comparing performance of:
Class method vs Symbol method
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
class Person { name = "j" getName() { return this.name } } window.__person = new Person() const _getName = Symbol() class Welder { name = "j"; [_getName]() { return this.name } } window._getName = _getName window.__welder = new Welder()
Tests:
Class method
const person = window.__person for (let i = 0; i < 100; i++) { person.getName() }
Symbol method
const welder = window.__welder const _getName = window._getName for (let i = 0; i < 100; i++) { welder[_getName]() }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Class method
Symbol method
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):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark measures the performance difference between using a normal class method and a Symbol method in JavaScript. **Options Compared** There are two options compared: 1. **Normal Class Method**: This is a traditional way of defining methods in classes, where the method name is a string. 2. **Symbol Method**: This uses Symbols as method names, which are unique and immutable strings. **Pros and Cons of Each Approach** * **Normal Class Method**: + Pros: Easy to understand and maintain, widely supported by most browsers and environments. + Cons: May lead to slower performance due to the overhead of string comparison during method lookup. * **Symbol Method**: + Pros: Can provide faster performance since Symbols are compared using a hash-based lookup, which is generally faster than string comparison. + Cons: Requires more memory to store the Symbol instance, and may be less intuitive for some developers. **Library and Special JS Features** The benchmark uses two libraries/technologies: 1. **Symbol**: A built-in JavaScript feature introduced in ECMAScript 2015 (ES6), which allows creating unique and immutable string-like objects. 2. **Window.__person** and `window.__welder`: These are global variables used to store instances of the `Person` and `Welder` classes, respectively. This is a common pattern in JavaScript for sharing data between scripts. The benchmark also uses some special JS features: 1. **Class Syntax**: The benchmark defines two classes, `Person` and `Welder`, which use the class syntax introduced in ECMAScript 2015 (ES6). 2. **For Loops**: The benchmark uses `for` loops to execute the test cases multiple times. **Other Considerations** * **Browser Support**: The benchmark is run on Firefox 116, a popular browser that supports both Symbol methods and normal class methods. * **Device Platform**: The benchmark is run on a desktop platform, which may not accurately represent mobile or tablet devices. * **Operating System**: The benchmark is run on Windows, which may not accurately represent other operating systems. **Alternatives** Other alternatives for measuring the performance difference between Normal Class Method and Symbol Method include: 1. Using a different browser or environment to ensure compatibility and fairness. 2. Adding more test cases with varying input sizes and complexity. 3. Incorporating additional optimization techniques, such as caching or memoization, to further improve performance. I hope this explanation helps software engineers understand the benchmark and its results!
Related benchmarks:
function vs class method vs new function method
function vs class method vs new function method v2
Object creation: arrow function vs. class with methods
Comparison of classes vs prototypes vs object literals also including the inheritance
Comments
Confirm delete:
Do you really want to delete benchmark?