Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
createElement vs cloneNode vs JQuery
(version: 0)
Faster way to create new dom elements before insertion
Comparing performance of:
createElement vs cloneNode vs JQuery vs JQuery(cloneNode)
Created:
5 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>
Tests:
createElement
var list = [], n = 0; while(true) { n++; list.push(document.createElement('div')); if(n===100000) break; }
cloneNode
var list = [], n = 0, node = document.createElement('div'); while(true) { n++; list.push(node.cloneNode(true)); if(n===100000) break; }
JQuery
var list = [], n = 0; while(true) { n++; list.push($('<div>')); if(n===100000) break; }
JQuery(cloneNode)
var list = [], n = 0, node = document.createElement('div'); while(true) { n++; list.push($(node.cloneNode(true))); if(n===100000) break; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
createElement
cloneNode
JQuery
JQuery(cloneNode)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
20 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
createElement
43.1 Ops/sec
cloneNode
88.6 Ops/sec
JQuery
40.4 Ops/sec
JQuery(cloneNode)
68.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down what's being tested in this benchmark, explain the pros and cons of different approaches, and describe any libraries or special features used. **Benchmark Overview** The benchmark compares three ways to create new DOM elements: `createElement`, `cloneNode`, and using jQuery. The goal is to determine which method is faster for creating a large number of elements before insertion into the DOM. **Test Cases** Each test case has a unique benchmark definition, but they all aim to measure the performance of one of these three methods: 1. **`createElement`**: Creates a new `div` element using the `document.createElement()` method. 2. **`cloneNode`**: Clones an existing `div` element using the `cloneNode(true)` method and pushes the cloned node into an array. 3. **`JQuery`** (and its variant `JQuery(cloneNode)`): Uses jQuery to create a new `div` element using the `$` function, which is a shorthand for `jQuery()`. The `cloneNode` variation uses this same approach with cloned elements. **Libraries and Special Features** * **jQuery**: A JavaScript library used for DOM manipulation, events, and other tasks. In this benchmark, jQuery is used to create new elements using its `$` function. * **Special feature:** The use of the ` cloneNode(true)` method, which creates a deep clone of an element by recursively cloning all child nodes. **Approach Pros and Cons** 1. **`createElement`**: * Pros: Simple, lightweight, and easy to implement. * Cons: May require additional DOM operations (e.g., appending elements to the DOM) after creation. 2. **`cloneNode`**: * Pros: Can be faster than `createElement` for large numbers of elements, as it reuses existing node structures. * Cons: Requires a separate cloned element, which can add overhead. 3. **`JQuery` (and `JQuery(cloneNode)`)**: * Pros: Abstraction layer provides a simple and convenient way to work with DOM elements, making it easier to create large numbers of elements. * Cons: Adds jQuery's library footprint, which may introduce additional overhead compared to plain JavaScript implementations. **Other Alternatives** If you don't have access to jQuery or prefer not to use it, other alternatives for creating new DOM elements could include: 1. **`DOMParser`**: Creates a new document and allows you to create elements within it. 2. **`HTMLTemplates`**: A newer JavaScript API that provides a simple way to create HTML templates and generate new elements from them. Keep in mind that these alternatives may not provide the same level of convenience as jQuery, but they can be useful for specific use cases or when working with older browsers that don't support modern JavaScript features.
Related benchmarks:
createElement vs cloneNode(false)
createElement vs cloneNode v3
createElement vs cloneNode vs cloneNode-lite
createElement vs deep cloneNode vs cloneNode
Comments
Confirm delete:
Do you really want to delete benchmark?