开发者

Is it possible to make this script more efficient?

I have just finished my script, using flot and jquery. Now to my question, it is fast in opera and Firefox, but it is painfully slow in internet explorer (no surprise), so thats why I wonder if there is a way to make my script more efficient (In other words perhaps remove some of the "for loops" etc)? So if there are any code gurus out there who have some spare time to kill, please help me out, because I mysel开发者_如何学JAVAf am terrible at writing efficient code :P

Thanks so much in advance =)

It can be found on this address


A few more tips:

It was pointed out that:

... $(this).attr('id');
... $(this).attr('name');

is expensive, you don't need $ here at all, just use:

... this.id;
... this.name;

Also, using .css(...) is a huge waste, use a class and put the CSS in an style element.

You can store references like $('#x') in closures. Again, you don't need $, it's far more efficient to get a reference directly to the element using document.getElementByid so that rather than:

$('#x').text(pos.x.toFixed(2));

you can have:

x.innerHTML = pos.x.toFixed(2);

which replaces several function calls with a single property access. The basic idea is to remove as much jQuery as you can, keep references to things rather than getting them frequently and use direct property access, not functions.

Incidentally, when I try to copy from the jsFiddle javascript region, Safari freezes. I'm not a big fan of that site.


 for(k; k<datasets.length; k++){

Every time the loop is executed next, you are calling the length property, it's better if you store it in a variable at the start of the loop only, like this:

for(var k, len = datasets.length; k < len; k++){

Also here you are wasting resources:

key = $(this).attr("id");
subsystem = $(this).attr("name");

just stich $(this) into a variable, cause every time you use $() a clone of the passed element is created. Just do like this:

var $this = $(this);

And use $this from there on instead of $(this), only reuse $(this) when this become a different object.


Firstly, with jQuery selectors, if using a classname, it's more efficient if you can make that more specific. e.g instead of

var checkboxContainerFailed = $(".failedCheckboxes");

try

var checkboxContainerFailed = $("#graph-table-bottom td.failedCheckboxes");

Secondly, it's generally considered better to use [] notation instead of var subsystemNames = new Array();

Thirdly, you have a trailing comma in the data array here. This might cause IE problems:

"test2-a4/",
        ]

Finally, try running the whole thing through JSLint for any errors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜