Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
function vs class 8
(version: 0)
Comparing performance of:
function declaration vs function expression vs class
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [];
Tests:
function declaration
function functionDeclaration(item) { arr.push(item); } for (let i = 1; i < 10000; i++) { functionDeclaration(i); }
function expression
var functionExpression = function(item) { arr.push(item); }; for (let i = 1; i < 10000; i++) { functionExpression(i); }
class
class Test { constructor() { } addItem(item){ arr.push(item) } } var test = new Test(); for (let i = 1; i < 10000; i++) { test.addItem(); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
function declaration
function expression
class
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
gemma2:9b
, generated one year ago):
This benchmark compares the performance of three different ways to define and use functions in JavaScript: **1. Function Declaration:** * `function functionDeclaration(item) { ... }` This is the classic way to define a function in JavaScript, where you use the `function` keyword followed by the name of the function, parentheses for parameters, and curly braces to enclose the code block. **2. Function Expression:** * `var functionExpression = function(item) { ... };` This defines a function within an expression. The `function` keyword is used inside another expression, often assigned to a variable. **3. Class:** * `class Test { ... }` Classes are objects that can have properties and methods. This test uses a simple class with a single method (`addItem`) that pushes an item into the shared array. **What is being measured?** The benchmark measures how many times each function definition can be called within a specific time frame (in this case, executions per second). The script iterates from 1 to 9999, calling the provided function for each number. **Pros and Cons:** * **Function Declarations:** Generally have good performance, as they are hoisted (can be called before their definition in the code). * **Function Expressions:** Can be more flexible for dynamic creation of functions based on conditions. * **Classes:** Provide a more structured way to organize code, especially for larger projects. However, class instantiation and method calls can introduce some overhead compared to simple function calls. **Other Considerations:** * **Context:** This benchmark doesn't capture the real-world performance differences that might arise in different application contexts. Factors like caching, memory management, and other parts of your codebase heavily influence execution speed. * **Libraries:** This test doesn't utilize any external libraries. **Alternatives:** * Benchmarking tools: Instead of using MeasureThat.net, consider more comprehensive benchmarking tools like Benchmark.js or WebPageTest which offer more in-depth analysis and a wider range of tests.
Related benchmarks:
Array constructor vs literal performance, 12345
arr.slice(-1)[0] vs arr[arr.length - 1]
arr.at(-1) vs arr[arr.length - 1]
Array .push() vs .unshift() |
Array add vs .unshift()
Comments
Confirm delete:
Do you really want to delete benchmark?