Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new class from fn vs function returning obj from fn
(version: 0)
Measure the performance of instanceOf operator vs comparing a basic string type.
Comparing performance of:
new class vs new fn
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script> function getClass(){ return class TestClass { constructor() { this.a = 2; this.t = 'mesh'; } } } function getFn(){ return function TestFn(){ return { a:2, t:'mesh' }; } } </script>
Tests:
new class
var obj = new (getClass())();
new fn
var obj = getFn()();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new class
new fn
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new class
980857.6 Ops/sec
new fn
20646274.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is testing two different approaches to creating an object in JavaScript: 1. **New class from function**: This approach uses the `class` keyword to define a new class `TestClass`, which has a constructor that initializes two properties: `a` and `t`. The `new` keyword is used to create an instance of this class, which is stored in the variable `obj`. 2. **Function returning object from function**: This approach uses a function `getFn()` that returns another function `TestFn()`. When called, `TestFn()` returns an object with two properties: `a` and `t`. **Options Compared** The benchmark is comparing the performance of these two approaches: * Creating an instance of a class using the `new` keyword (`new class from fn`) * Using a function to return an object directly (`function returning obj from fn`) **Pros and Cons** * **New Class from Function**: + Pros: Can be more readable and maintainable, especially for complex classes. The `class` keyword provides type safety and auto-generated methods (e.g., `toString()`). + Cons: May have performance overhead due to the complexity of class creation. * **Function Returning Object from Function**: + Pros: Simple and lightweight, which can lead to better performance in some cases. This approach is often used in functional programming styles. + Cons: Less readable and maintainable, as it relies on function composition. **Library and Syntax** The benchmark uses the `class` keyword, which is a part of ECMAScript 2015 (ES6) standard. The `function returning object from function` approach also uses ES6 features, specifically function expressions and arrow functions. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in this benchmark beyond the ones mentioned above. **Other Alternatives** Other approaches to creating objects in JavaScript include: * **Constructor Functions**: Using a function with the `new` keyword to create an object. (e.g., `function TestFn() { ... }`) * **Object Prototypes**: Creating objects by extending an existing prototype or using `Object.create()`. (e.g., `var obj = Object.create(TestClass.prototype);`) * **Prototype Chains**: Creating objects by chaining method calls on a base object. These alternatives may have different performance characteristics, readability, and maintainability compared to the approaches tested in this benchmark.
Related benchmarks:
Function: typeof vs instanceof
InstanceOf vs String type 2
new class vs function returning obj
new class from fn vs function returning obj from fn 2
Comments
Confirm delete:
Do you really want to delete benchmark?