Я должен использовать jQuery.inArray ()?

Этот код получает 242 страны в Sun Java 6:

String[] countryCodes = Locale.getISOCountries();

, Хотя веб-сайт ISO требования там 249 элементы кода ISO 3166-1-alpha-2 , хотя javadoc связывается с той же информацией.

10
задан hakre 9 September 2013 в 12:51
поделиться

2 ответа

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;
}

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.

15
ответ дан 3 December 2019 в 17:20
поделиться

Even is the inArray function were slow, you're still creating a full new array for every search. I suppose it would be better to redesign this search, by e.g. creating the id-list before finding the points, and using that one to search into:

2
ответ дан 3 December 2019 в 17:20
поделиться
Другие вопросы по тегам:

Похожие вопросы: