Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
mutation vs builder
(version: 0)
Comparing performance of:
mutation vs Builder
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
mutation
function Point(x, y) { this.x = x this.y = y } Point.prototype.add = function(point) { this.x = this.x + point.x this.y = this.y + point.y } var p1 = new Point(1, 2) var p2 = new Point(3, 4) p1.add(p2) p1.add(p2) p1.add(p2) p1.add(p2) p1.add(p2) p1.add(p2) p1.add(p2) p1.add(p2) p1.add(p2) p1.add(p2) p1.add(p2) p1.add(p2) p1.add(p2) p1.add(p2)
Builder
function Point(x, y) { this.x = x this.y = y } Point.prototype.add = function(point) { return new Point(this.x + point.x, this.y + point.y) } var p1 = new Point(1, 2) var p2 = new Point(3, 4) p1.add(p2).add(p2).add(p2).add(p2).add(p2).add(p2).add(p2).add(p2).add(p2).add(p2).add(p2).add(p2).add(p2).add(p2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
mutation
Builder
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):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares two approaches: mutation and builder, for adding points together in an object. **Tested Options** The test compares two options: 1. **Mutation**: This approach modifies the existing `Point` object by incrementing its `x` and `y` properties directly. 2. **Builder**: This approach creates a new `Point` object with the incremented values instead of modifying the original one. **Pros and Cons** * **Mutation**: + Pros: Simple, easy to implement, and may be faster since it avoids creating a new object. + Cons: Can lead to unexpected behavior if the original object is modified unexpectedly, and may not preserve state between calls. * **Builder**: + Pros: Preserves state between calls, avoids modifying the original object, and can be safer in certain situations (e.g., when working with shared objects). + Cons: May be slower since it creates a new object for each call, and requires more code to implement. **Library and Purpose** In this benchmark, the `Point` class is defined as a simple JavaScript object with two properties (`x` and `y`). The `add` method on the `Point` prototype either modifies the existing object (mutation) or returns a new `Point` object (builder). **Special JS Feature/Syntax** There are no special JS features or syntaxes used in this benchmark, aside from ES6-style class definition for the `Point` class and arrow functions for the `add` method. **Other Considerations** When choosing between mutation and builder approaches, consider the following: * Use mutation when you need to modify an existing object and don't care about preserving state or safety. * Use builder when you want to preserve state between calls, avoid modifying the original object, or work with shared objects. **Alternatives** Other alternatives for adding points together in an object include: 1. Using a separate `PointFactory` class that creates new `Point` objects instead of modifying the existing one. 2. Implementing the `add` method as a simple arithmetic operation (e.g., `this.x += point.x; this.y += point.y;`) without creating a new object. 3. Using a third-party library or framework that provides a built-in `Point` class with an `add` method. Keep in mind that each of these alternatives may have trade-offs and considerations specific to your use case.
Related benchmarks:
Object.prototype.hasOwnProperty vs obj.hasOwnProperty
clone with change
json stringify vs string tostring
Test String() vs JSON.stringify()
bmm tests2
Comments
Confirm delete:
Do you really want to delete benchmark?