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

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

问题描述

我的对象数组做的非常频繁的搜索,并已使用jQuery.inArray()。不过,我在速度和内存的问题,根据我的探查最调用的方法之一就是jQuery.inArray()。什么是对关于其性能街道上的字?我应该切换到一个简单的for循环?

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

我的具体功能是:

function findPoint(point, list)
{
  var l = list.map(function anonMapToId(p) { return p.id });
  var found = jQuery.inArray(point.id, l);
  return found;
}

也许 list.map()更怪?

推荐答案

好内部 inArray 做一个简单的循环,我会建议你检查是否有本地<一个href=\"https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf\"><$c$c>Array.prototype.indexOf实施和使用它来代替 inArray 的(如果可用):

Well internally inArray makes a simple loop, I would recommend you to check if there is a native Array.prototype.indexOf implementation and use it instead of inArray if available:

function findPoint(point, list) {
  var l = list.map(function anonMapToId(p) { return p.id });
  var found = ('indexOf' in Array.prototype) ? l.indexOf(point.id)
                                             : jQuery.inArray(point.id, l);
  return found;
}

Array.prototype.indexOf 方法在实现JavaScript的浏览器1.6已经出台,这将是ECMAScript的5个标准的组成部分。

The Array.prototype.indexOf method has been introduced in browsers that implement JavaScript 1.6, and it will be part of the ECMAScript 5 standard.

本机实现的办法比非原生得更快。

Native implementations are way faster than non native ones.

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

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