Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Classes vs functions fork
(version: 0)
Comparing performance of:
Class vs Fn
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
Class
var Test = /** @class */ (function () { function Test(x) { this.x = x; } Test.prototype.add = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract1 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract2 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract3 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract4 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract5 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract16 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract26 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract36 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract46 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.add55 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract55 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract144 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract233 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract313 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract413 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract51 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; Test.prototype.subtract161 = function () { var x = this.x; return new Test(Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); }; return Test; }()); new Test(34235345345342342323423422334423).add();
Fn
function add(x) { return (Math.pow(x, 10) + Math.exp(x) + Math.sin(x) - Math.cos(x)); } add(34235345345342342323423422334423);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Class
Fn
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):
The provided benchmark measures the performance of two approaches: classes and functions for creating a simple mathematical expression. **Classes vs Functions** In this benchmark, both approaches are compared for their execution speed. The class-based approach uses a JavaScript class definition to create an instance of a class with a specific implementation, whereas the function-based approach uses a regular function definition without classes. Let's break down each option: ### Class-Based Approach The class-based approach creates a JavaScript class called `Test` that has several methods: `add`, `subtract`, and others. Each method takes some input `x` and returns a new instance of the `Test` class with a modified implementation. This process involves: 1. Creating a new instance of the `Test` class. 2. Calling the `add`, `subtract`, or other methods on that instance. The pros of this approach are: * Encapsulation: The code is organized into a clear structure, making it easier to understand and maintain. * Reusability: The same implementation can be reused across multiple instances of the class. However, there are some cons: * Complexity: Creating a new instance of the `Test` class involves more overhead than calling a function directly. * Overhead: The process of creating an instance and calling methods on it may introduce unnecessary complexity. ### Function-Based Approach The function-based approach uses a regular function definition to create a simple mathematical expression. This approach is similar to the class-based one, but instead of using a class definition, it defines a single function `add` that takes some input `x` and returns a new value. The pros of this approach are: * Simplicity: Defining a single function is much simpler than creating a class with multiple methods. * Directness: Calling the function directly is more straightforward than calling methods on an instance. However, there are some cons: * Lack of encapsulation: The code is not organized into a clear structure, making it harder to understand and maintain. * Reusability: The same implementation may need to be repeated across multiple calls to the function. **Benchmark Results** The latest benchmark results show that the function-based approach (`Fn`) outperforms the class-based approach (`Class`). In this case, the Chrome 87 browser executed the `Fn` function approximately 18 times faster than the `Class` instance creation and method call. Keep in mind that these results may vary depending on the specific use case, hardware, and other factors.
Related benchmarks:
Class instance method lookup vs function-created object method lookup (fork)
prototype methods vs defined function
Creation time smaller function pointing to prototype methods VS one larger all in one funtion
Comparison of classes vs prototypes vs object literals also including the inheritance
Wrapping objects method removal performance
Comments
Confirm delete:
Do you really want to delete benchmark?