我应该使用jQuery.each()? [英] Should I use jQuery.each()?

查看:108
本文介绍了我应该使用jQuery.each()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在对象的数组做的非常频繁的迭代,并一直在使用jQuery.each()。不过,我在速度和内存的问题,根据我的探查最调用的方法之一就是jQuery.each()。什么是对关于其性能街道上的字?我应该切换到一个简单的for循环?当然,我在固定我的code中的许多问题了。

I'm doing very frequent iterations over arrays of objects and have been using jQuery.each(). However, I'm having speed and memory issues and one of the most called methods according to my profiler is jQuery.each(). What's the word on the street about its performance? Should I switch to a simple for loop? Of course I'm fixing the many issues in my own code too.

推荐答案

来源$ C ​​$ C为jQuery的每个如下(谢谢约翰Resig的和麻省理工学院许可证):

The source code for jQuery's each is as follows (Thank you John Resig and MIT License):

each: function( object, callback, args ) {
	var name, i = 0, length = object.length;

	if ( args ) {
		if ( length === undefined ) {
			for ( name in object )
				if ( callback.apply( object[ name ], args ) === false )
					break;
		} else
			for ( ; i < length; )
				if ( callback.apply( object[ i++ ], args ) === false )
					break;

	// A special, fast, case for the most common use of each
	} else {
		if ( length === undefined ) {
			for ( name in object )
				if ( callback.call( object[ name ], name, object[ name ] ) === false )
					break;
		} else
			for ( var value = object[0];
				i < length && callback.call( value, i, value ) !== false; value = object[++i] ){}
	}

	return object;
}

正如你可以跟踪看到,在大多数情况下,唯一的开销是真的只是回调本身它使用的是基本的for循环。不得在性能差异。

As you can trace and see, in most cases it is using a basic for loop where the only overhead is really just the callback itself. Shouldn't make a difference in performance.

编辑:这是认识到选择的开销已经发生并且您将得到一个人口稠密的阵列对象

This is with the realization that selector overhead has already occurred and you are given a populated array object.

这篇关于我应该使用jQuery.each()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆