How to Benchmark Javascript DOM Manipulation -
i have 2 javascript functions same thing: create menu based on json object.
one function appends <ul>
, <li>
elements variable, , writes html document using method innerhtml
the second function create dom elements through createelement("ul")
, appendchild()
methods
so want know function faster, not know how perform benchmark test in javascript.
my first function buildmenutostring()
, second function buildmenudom()
i use this:
var bench = function(fn, iterations){ var time = 0, = 0, total; // start time = +(new date); while(i < iterations){ fn.apply(); i++; } total = +(new date) - time; console.log("mean exec time: ", total / iterations, 'ms'); console.log("sum exec time: ", total, 'ms'); };
var test1 = function(){ $('body').append('<div />'); }, test2 = function(){ div = document.createelement('div'); document.body.appendchild(div); }; bench(test1, 1000); bench(test2, 1000);
Comments
Post a Comment