Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
es6 class static vs instance vs object method
(version: 0)
Comparing performance of:
function vs object vs static vs instance
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function bar() {} var object = { bar() {} } var Foo = class Foo { static bar() {} constructor () {} bar() {} } var foo = new Foo()
Tests:
function
bar();
object
object.bar();
static
Foo.bar();
instance
foo.bar();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
function
object
static
instance
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Android 12; Mobile; rv:149.0) Gecko/149.0 Firefox/149.0
Browser/OS:
Firefox Mobile 149 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
function
583290944.0 Ops/sec
object
583389376.0 Ops/sec
static
562930368.0 Ops/sec
instance
582341824.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark tests the performance of different ways to call a method in JavaScript. It compares: * **`function()`**: A regular function declaration. * **`object.bar();`**: Accessing and calling a method on an object literal. * **`Foo.bar();`**: Calling a static method (a method belonging to the class itself, not an instance of the class) defined within a ES6 class. * **`foo.bar();`**: Calling an instance method (a method belonging to an individual object created from a class) on an instance of an ES6 class. **Pros and Cons:** * **`function()`**: * **Pro:** Simple, straightforward syntax. * **Con:** Can be less organized for larger projects with many functions. * **`object.bar();`**: * **Pro:** Direct access to methods within an object. * **Con:** Can lead to more verbose code when dealing with multiple objects and methods. * **`Foo.bar();`**: * **Pro:** Good for utility functions or operations related to the class itself, avoids creating instances. * **Con:** Not suitable for methods that operate on specific object properties. * **`foo.bar();`**: * **Pro:** Encapsulates data and behavior within objects, promotes code reusability. * **Con:** Can be more complex to set up compared to simpler function calls. **Other Considerations:** The benchmark results indicate that using static methods (`Foo.bar()`) or instance methods (`foo.bar()`) of an ES6 class are generally faster than calling a regular function or accessing methods on object literals. This suggests that the JavaScript engine might be more optimized for working with classes and their methods. **Alternatives:** While not explicitly shown in this benchmark, other ways to call methods include: * **Arrow Functions**: A concise syntax introduced in ES6. They often offer slight performance advantages over regular function expressions, but it's a subtle difference. Keep in mind that benchmark results can vary depending on factors like the browser, JavaScript engine, and specific code implementation.
Related benchmarks:
function vs class method vs new function method
function vs class method vs new function method v2
ES6 Class vs Prototype vs Object Literal v2
class vs function constructor vs object literal vs __proto__ vs Object.create vs Object.setPrototypeOf
Comments
Confirm delete:
Do you really want to delete benchmark?