Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
clone vs cloneNode
(version: 0)
Comparing performance of:
clone vs cloneNode
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script> <div></div> <div class='hoge'>ああああああああああああ</div> <a id='' href='#' />
Script Preparation code:
var $dw = $("div.hoge");
Tests:
clone
$dw.after( function() { return $(this).clone(); } );
cloneNode
$dw[0].insertAdjacentElement('afterend', $dw[0].cloneNode(true));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
clone
cloneNode
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
clone
99097.2 Ops/sec
cloneNode
771547.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of JavaScript microbenchmarks is crucial for optimizing code and identifying areas for improvement. Let's break down the provided benchmark definition and test cases: **Benchmark Definition** The benchmark definition JSON represents two test cases: `clone` and `cloneNode`. The script preparation code includes a jQuery variable `$dw` which points to an HTML element with class `hoge`. **Test Cases** 1. **`clone`**: This test case measures the performance of cloning an element using the `after` method. ```javascript $dw.after(function() { return $(this).clone(); }); ``` The `after` method is used to attach a function to be executed after another function has finished executing. In this case, the cloned element is returned. 2. **`cloneNode`**: This test case measures the performance of cloning an element using the `insertAdjacentElement` method with `cloneNode(true)`. ```javascript $dw[0].insertAdjacentElement('afterend', $dw[0].cloneNode(true)); ``` The `insertAdjacentElement` method is used to insert a child element into the DOM. The `cloneNode(true)` method creates a deep clone of the original element. **Library and Purpose** In both test cases, jQuery is used as the library. Its purpose is to simplify DOM manipulation, event handling, and other tasks by providing a convenient API for interacting with the document object model (DOM). **Special JS Feature/Syntax** Neither of these tests uses any special JavaScript features or syntax beyond what's standard in modern JavaScript. **Pros and Cons** 1. **`clone`**: Using `after` to clone an element has some advantages: * It allows for easy integration with other jQuery methods, like `before()` and `done()`. * The cloned element is returned, which can be useful for further processing. However, it also has some drawbacks: * It might not work as expected in older browsers that don't support the `after` method. * Cloning an element using `after` creates a new DOM node, which can lead to performance issues if not done carefully. 2. **`cloneNode`**: Using `insertAdjacentElement` with `cloneNode(true)` has its own set of advantages and disadvantages: * It's generally faster than cloning an element using `after`, as it avoids the overhead of creating a new DOM node. * However, it returns null, which might be unexpected behavior for some developers. **Other Alternatives** For measuring performance in JavaScript benchmarks, you can consider other alternatives like: 1. **V8 Benchmark Suite**: A collection of benchmarking tests specifically designed to measure the performance of V8, the JavaScript engine used by Google Chrome. 2. **JSPerf**: A web-based tool for comparing the performance of JavaScript code snippets. 3. **Benchmark.js**: A lightweight library for running benchmarks in Node.js and browser environments. Keep in mind that each benchmarking tool has its own strengths and weaknesses, and choosing the right one depends on your specific needs and requirements.
Related benchmarks:
Object cloning with Lodash clone vs cloneDeep vs merge vs ES6 object spread vs ES6 Object.assign
Object cloning with Lodash clone vs cloneDeep vs merge vs ES6 object spread vs ES6 Object.assign BIG
Object cloning with Lodash clone vs cloneDeep vs merge vs ES6 object spread vs ES6 Object.assign vs Clone
Object cloning with Lodash clone vs cloneDeep vs merge vs assign vs ES6 object spread vs ES6 Object.assign AWD AWd AWd AWd AWd Wd AW d
Object cloning with Lodash clone vs cloneDeep vs merge vs ES6 object spread vs ES6 Object.assign vs structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?