Skip to content

Instantly share code, notes, and snippets.

@artjomb
Last active August 29, 2015 14:06
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 artjomb/ee6d592c69806d4c2e60 to your computer and use it in GitHub Desktop.
Save artjomb/ee6d592c69806d4c2e60 to your computer and use it in GitHub Desktop.
Test PhantomJS 1.9.7 selectors API speed for a huge DOM
var page = require('webpage').create();
var content = "",
max = 100000,
i;
for(i = 0; i < max; i++) {
content += '<form id="f' + i + '"><input type="hidden" name="in' + i + '" valuate"iv' + i + '"></form>';
}
page.evaluate(function(content){
document.body.innerHTML = content;
}, content);
console.log("FORMS ADDED");
setTimeout(function(){
var times = page.evaluate(function(max){
var obj = {
cssplain: 0,
cssbyForm: 0,
cssbyFormChild: 0,
cssbyFormJsDomChild: 0,
cssbyFormChildHybridChild: 0,
cssbyFormHybridChild: 0,
xpathplain: 0,
xpathbyForm: 0
},
idx, start, el, i,
repeat = 100;
for(i = 0; i < repeat; i++){
idx = Math.floor(Math.random()*max);
start = (new Date()).getTime();
el = document.querySelector('input[name="in'+idx+'"][value="iv'+idx+'"]');
obj.cssplain += (new Date()).getTime() - start;
idx = Math.floor(Math.random()*max);
start = (new Date()).getTime();
el = document.querySelector('#f'+idx+' input[name="in'+idx+'"][value="iv'+idx+'"]');
obj.cssbyForm += (new Date()).getTime() - start;
idx = Math.floor(Math.random()*max);
start = (new Date()).getTime();
el = document.querySelector('form:nth-child('+(idx+1)+') input[name="in'+idx+'"][value="iv'+idx+'"]');
obj.cssbyFormChild += (new Date()).getTime() - start;
idx = Math.floor(Math.random()*max);
start = (new Date()).getTime();
el = document.body.children[max-1].querySelector('input[name="in'+idx+'"][value="iv'+idx+'"]');
obj.cssbyFormJsDomChild += (new Date()).getTime() - start;
idx = Math.floor(Math.random()*max);
start = (new Date()).getTime();
el = document.querySelector('form:nth-child('+(idx+1)+')').querySelector('input[name="in'+idx+'"][value="iv'+idx+'"]');
obj.cssbyFormChildHybridChild += (new Date()).getTime() - start;
idx = Math.floor(Math.random()*max);
start = (new Date()).getTime();
el = document.querySelector('#f'+idx).querySelector('input[name="in'+idx+'"][value="iv'+idx+'"]');
obj.cssbyFormHybridChild += (new Date()).getTime() - start;
idx = Math.floor(Math.random()*max);
start = (new Date()).getTime();
el = document.evaluate('//input[@name="in'+idx+'" and @value="iv'+idx+'"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
obj.xpathplain += (new Date()).getTime() - start;
idx = Math.floor(Math.random()*max);
start = (new Date()).getTime();
el = document.evaluate('//form[@id="f'+idx+'"]//input[@name="in'+idx+'" and @value="iv'+idx+'"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
obj.xpathbyForm += (new Date()).getTime() - start;
}
for(var type in obj) {
obj[type] /= repeat;
}
return obj;
}, max);
console.log("TIMES");
for(var type in times) {
console.log(type+":\t"+times[type]);
}
phantom.exit();
}, 0); // just in case the content is not yet evaluated
cssbyForm: 29.55
cssbyFormChild: 29.97
cssbyFormChildHybridChild: 11.51
cssbyFormHybridChild: 10.17
cssbyFormJsDomChild: 11.73
cssplain: 29.39
xpathbyForm: 206.66
xpathplain: 207.05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment