Lg102
Lg102

Reputation: 4908

javascript speed

On an average system, say

Processor: Core 2 Duo 2.2 GHz, Memory: 4 GB

How much DOM-manipulations can javascript make? Is there a way to see how long (in milliseconds) a simple jQuery action takes? For example one like:

$("#example").css("color","red");

Upvotes: 1

Views: 151

Answers (3)

Ry-
Ry-

Reputation: 225164

var start = Date.now();

for(var i = 0; i < 10000; i++) { // any number of repetitions
    $("#example").css("color", "red");
}

var end = Date.now();
alert("It took " + Math.round((end - start) / 10000).toString() + " milliseconds on average.");

is how you'd time it. I don't know about DOM actions. You can look in the sizzle.js source to find out exactly what it does, though I know it optimizes for ID selectors and then it depends on the browser. Maybe they traverse the tree in order until they find a matching id. It would be pretty fast.

Upvotes: 1

Francois Deschenes
Francois Deschenes

Reputation: 24989

If you're browser supports it, you could use console.profile(); before the call(s) and console.profileEnd(); after. You console should show you output how long it took, the number of calls, etc.

console.profile();
$("#example").css("color","red");
console.profileEnd();

Nettuts+ has a tutorial here.

Upvotes: 3

George Cummins
George Cummins

Reputation: 28936

The process of making these determinations is called "profiling." You can find profilers built into the Chrome Developer Tools, and Firebug for Firefox.

Upvotes: 3

Related Questions