Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active July 3, 2023 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/806c58bcef20474b7f8117f3290eb00a to your computer and use it in GitHub Desktop.
Save code-boxx/806c58bcef20474b7f8117f3290eb00a to your computer and use it in GitHub Desktop.
Javascript HTML Document Fragment

JAVASCRIPT HTML DOCUMENT FRAGMENT

https://code-boxx.com/javascript-document-fragment/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Document Fragment Example</title>
</head>
<body>
<!-- (A) LIST TO APPEND TO -->
<ul id="thelist">
<li>Apple - A juicy red fruit, sometimes green.</li>
</ul>
<!-- (B) JAVASCRIPT -->
<script>
// (B1) DATA TO ADD
var data = {
Banana : "Long yellow fruit. Priced at $120,000 with a duct tape.",
Cherry : "Small red berry thing.",
Dragonfruit : "Not a dragon but a fruit.",
Elderberry : "Wise old berry, +5 intelligence."
};
// (B2) CREATE DOCUMENT FRAGMENT
// CAN BE CREATED WITH NEW DOCUMENTFRAGMENT() OR DOCUMENT.CREATEDOCUMENTFRAGMENT()
var frag = new DocumentFragment();
// var frag = document.createDocumentFragment();
// (B3) APPEND DATA TO FRAGMENT
for (let [k,v] of Object.entries(data)) {
let row = document.createElement("li");
row.innerHTML = `${k} - ${v}`;
frag.appendChild(row);
}
// (B4) APPEND FRAGMENT TO TABLE
document.getElementById("thelist").appendChild(frag);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Direct Insertion</title>
</head>
<body>
<!-- (A) TABLE TO APPEND TO -->
<table id="thetable"></table>
<!-- (B) JAVASCRIPT -->
<script>
// (B1) START TIMING
var start = new Date();
// (B2) GENERATE A LOT OF TABLE ROWS + CELLS
var thetable = document.getElementById("thetable");
for (let i=1; i<=1000; i++) {
let row = document.createElement("tr"),
cellA = document.createElement("td"),
cellB = document.createElement("td"),
cellC = document.createElement("td"),
cellD = document.createElement("td");
cellA.innerHTML = "Cell A" + i;
cellB.innerHTML = "Cell B" + i;
cellC.innerHTML = "Cell C" + i;
cellD.innerHTML = "Cell D" + i;
row.appendChild(cellA);
row.appendChild(cellB);
row.appendChild(cellC);
row.appendChild(cellD);
thetable.appendChild(row);
}
// (B3) ELAPSED TIME
var time = new Date() - start;
console.log("Elapsed: " + time);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Fragment Insertion</title>
</head>
<body>
<!-- (A) TABLE TO APPEND TO -->
<table id="thetable"></table>
<!-- (B) JAVASCRIPT -->
<script>
// (B1) START TIMING
var start = new Date();
// (B2) GENERATE A LOT OF TABLE ROWS + CELLS
var frag = new DocumentFragment();
for (let i=1; i<=1000; i++) {
let row = document.createElement("tr"),
cellA = document.createElement("td"),
cellB = document.createElement("td"),
cellC = document.createElement("td"),
cellD = document.createElement("td");
cellA.innerHTML = "Cell A" + i;
cellB.innerHTML = "Cell B" + i;
cellC.innerHTML = "Cell C" + i;
cellD.innerHTML = "Cell D" + i;
row.appendChild(cellA);
row.appendChild(cellB);
row.appendChild(cellC);
row.appendChild(cellD);
frag.appendChild(row);
}
document.getElementById("thetable").appendChild(frag);
// (B3) ELAPSED TIME
var time = new Date() - start;
console.log("Elapsed: " + time);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment