Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
clone with change
(version: 2)
Comparing performance of:
cloneWith vs cloneWith2 vs mutate
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
class Holder { constructor(init) { Holder.internalAssign(this, init); } static has(instance, property) { return Object.prototype.hasOwnProperty.call(instance, property); } static internalAssign(target, source) { if (Holder.has(source, "a")) target.a = source.a; if (Holder.has(source, "b")) target.b = source.b; if (Holder.has(source, "c")) target.c = source.c; } clone() { return new Holder(this); } cloneWith(props) { const cloned = this.clone(); Holder.internalAssign(cloned, props); return cloned; } cloneWith2(props) { const cloned = this.clone(); Object.assign(this, props); return cloned; } mutate(mutation) { const cloned = this.clone(); mutation(cloned); return cloned; } } var initHolder = new Holder({ a: "value of a", b: "value of b", c: 1 });
Tests:
cloneWith
initHolder.cloneWith({ a: "changed a", b: "changed b" });
cloneWith2
initHolder.cloneWith2({ a: "changed a", b: "changed b" });
mutate
initHolder.mutate(x => { x.a = "changed a"; x.b = "changed b" });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
cloneWith
cloneWith2
mutate
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 and its test cases. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that tests the performance of three different methods for cloning and mutating an object: `cloneWith`, `cloneWith2`, and `mutate`. * The `clone` method creates a new instance of the `Holder` class, copying all its properties. * The `cloneWith` method creates a clone of the original object using `clone()`, and then updates it with new properties using `internalAssign`. * The `cloneWith2` method uses `Object.assign()` to update the cloned object with new properties. * The `mutate` method clones the original object using `clone()`, applies a transformation function to the clone, and returns the mutated object. **Options Compared** The benchmark compares the performance of these three methods: 1. **`cloneWith`**: Creates a new instance of the `Holder` class, copies all properties, and then updates it with new properties. 2. **`cloneWith2`**: Uses `Object.assign()` to update the cloned object with new properties. 3. **`mutate`**: Clones the original object using `clone()`, applies a transformation function to the clone, and returns the mutated object. **Pros and Cons of Each Approach** 1. **`cloneWith`**: Pros: * Can be more efficient when updating specific properties. * May have better cache locality due to direct property access. 2 Cons: * Requires an additional `internalAssign()` call, which may incur overhead. * **`cloneWith2`**: Pros: * Uses a standard method for updating objects (Object.assign()). * Can be more concise and easier to read. 3 Cons: * May have higher overhead due to the overhead of the `Object.assign()` function. * **`mutate`**: Pros: * Simplifies cloning and updating logic into a single method. * May reduce overall object creation and property assignment overhead. 4 Cons: * Requires applying a transformation function, which can be more complex. **Special Considerations** In the provided benchmark definition, there is no special JavaScript feature or syntax used beyond what's standard. However, in other scenarios, additional considerations might arise: * **ES6 Classes**: The `Holder` class uses ES6 classes, which provide a concise way to define objects and their behavior. * **Object Literals**: The benchmark uses object literals (`{}`) for defining objects, which is a standard JavaScript syntax. **Alternatives** If you wanted to implement an alternative clone or mutation method in this benchmark, some options could include: * Using a library like Lodash or Immutable.js for functional programming and immutable data structures. * Implementing a custom cloning or mutation algorithm based on specific requirements or performance characteristics.
Related benchmarks:
Manual clone versus prototype extend
Object.assign mutation vs spread
lodash _.cloneDeep() vs deppClone()
Lodash omit + cloneDeep vs JS forEach
Comments
Confirm delete:
Do you really want to delete benchmark?