Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
mutation vs builder v2
(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) console.log(p1)
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) var p3 = 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) console.log(p3)
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):
Let's break down the provided benchmark. **Benchmark Overview** The benchmark compares two approaches to add points together in JavaScript: mutation and builder patterns. The test case creates a `Point` class with an `add` method that either modifies the existing point (mutation) or returns a new point created by adding another point to it (builder). **Mutation Approach** In this approach, the `add` method of the `Point` class modifies the existing point's coordinates directly. This is done by assigning the sum of the current x-coordinate and the x-coordinate of the point being added. Pros: * Simple and straightforward implementation * Easy to understand for beginners Cons: * Modifies the original object, which can be problematic if the original object needs to remain unchanged * Can lead to unexpected behavior if not properly tested **Builder Approach** In this approach, the `add` method returns a new `Point` object created by adding another point to the existing one. This is done by creating a new instance of the `Point` class with the sum of the current x-coordinate and the x-coordinate of the point being added. Pros: * Does not modify the original object * Returns a new, independent object that can be used as needed Cons: * More complex implementation than the mutation approach * May require additional memory allocation for creating new objects **Library Used** The test case uses the `Point` class and its `add` method without any external libraries. However, it's worth noting that in a real-world scenario, you might want to use a more robust library or framework that provides similar functionality. **Special JS Feature/ Syntax** There is no special JavaScript feature or syntax used in this benchmark. **Benchmark Preparation Code** The script preparation code is empty, which means the test case uses default settings and doesn't require any specific setup or configuration. **Alternatives to Mutation and Builder Approaches** Other approaches to add points together might include: * Immutable data structures: Instead of modifying the original point, you could create a new object with the updated coordinates. This approach is similar to the builder pattern but uses immutable data structures. * Recursion: You could implement an `add` method that recursively creates new `Point` objects by adding another point to the existing one. * Functional programming: You might use functional programming techniques, such as map-reduce or closures, to create a new point object with the updated coordinates. Keep in mind that these alternatives might have their own trade-offs and complexities, but they can be useful in certain situations.
Related benchmarks:
Delete vs destructure for cloned objects
clone with change
Object creation: arrow function vs. class
Comparison of classes vs prototypes vs object literals also including the inheritance
bmm tests2
Comments
Confirm delete:
Do you really want to delete benchmark?