简体   繁体   中英

JQuery Document Ready Efficiency

Here's a question that I've been asking myself for a while. I have quite a large chunk of JQuery that's held within a single on ready function, like so:

$(function () {
... do operation 1
... do operation 2
... do operation 3
... do operation 4
... etc
});

Provided each operation is self-contained, is it more efficient to have one big function or to have multiple functions, for example:

$(function () {
... do operation 1
});
$(function () {
... do operation 2
});
$(function () {
... do operation 3
});
etc

Every duplicate of a code will result in a worse performance, in one way or another.

Always try to prevent duplicated in the code.

Here you can see a benchmark test between the 2 scenarios .

I didn't use jQuery in the benchmark because of limitations of the benchmark software, but you can get the idea.

Conclusion

There is no reason as much as I can think of, to split the code into multiple on ready functions, it will be less efficient because of duplication of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM