1 voie
Utilisez document.cloneNode()
La performance est :
Appel à document.cloneNode()
a pris ~0.22499999977299012 millisecondes.
et il y en aura peut-être d'autres.
var t0, t1, html;
t0 = performance.now();
html = document.cloneNode(true);
t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
html.documentElement.innerHTML = '<!DOCTYPE html><html><head><title>Test</title></head><body><div id="test1">test1</div></body></html>';
console.log(html.getElementById("test1"));
2 voies
Utilisez document.implementation.createHTMLDocument()
La performance est :
Appel à document.implementation.createHTMLDocument()
a pris ~0.14000000010128133 millisecondes.
var t0, t1, html;
t0 = performance.now();
html = document.implementation.createHTMLDocument("test");
t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
html.documentElement.innerHTML = '<!DOCTYPE html><html><head><title>Test</title></head><body><div id="test1">test1</div></body></html>';
console.log(html.getElementById("test1"));
3 voies
Utilisez document.implementation.createDocument()
La performance est :
Appel à document.implementation.createHTMLDocument()
a pris ~0.14000000010128133 millisecondes.
var t0 = performance.now();
html = document.implementation.createDocument('', 'html',
document.implementation.createDocumentType('html', '', '')
);
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
html.documentElement.innerHTML = '<html><head><title>Test</title></head><body><div id="test1">test</div></body></html>';
console.log(html.getElementById("test1"));
4 voies
Utilisez new Document()
La performance est :
Appel à document.implementation.createHTMLDocument()
a pris ~0.13499999840860255 millisecondes.
ParentNode.append
est une technologie expérimentale pour l'année 2020.
var t0, t1, html;
t0 = performance.now();
//---------------
html = new Document();
html.append(
html.implementation.createDocumentType('html', '', '')
);
html.append(
html.createElement('html')
);
//---------------
t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
html.documentElement.innerHTML = '<html><head><title>Test</title></head><body><div id="test1">test1</div></body></html>';
console.log(html.getElementById("test1"));