Skip to main content
added 54 characters in body
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

It depends on your purpose. If you program for the Web, avoid indexOfindexOf, it isn't supported by IE6Internet Explorer 6 (lot of them still used!), or do conditional use:

if (yourArray.indexOf !== undefined) result = yourArray.indexOf(target); else result = customSlowerSearch(yourArray, target); 

indexOfindexOf is probably coded in native code, so it is faster than anything you can do in JSJavaScript (except binary search/dichotomy if the array is appropriate). Note: it is a question of taste, but I would do a return false; at the end of your routine, to return a true booleanBoolean...

It depends on your purpose. If you program for the Web, avoid indexOf, it isn't supported by IE6 (lot of them still used!), or do conditional use:

if (yourArray.indexOf !== undefined) result = yourArray.indexOf(target); else result = customSlowerSearch(yourArray, target); 

indexOf is probably coded in native code, so faster than anything you can do in JS (except binary search/dichotomy if the array is appropriate). Note: question of taste, but I would do a return false; at the end of your routine, to return a true boolean...

It depends on your purpose. If you program for the Web, avoid indexOf, it isn't supported by Internet Explorer 6 (lot of them still used!), or do conditional use:

if (yourArray.indexOf !== undefined) result = yourArray.indexOf(target); else result = customSlowerSearch(yourArray, target); 

indexOf is probably coded in native code, so it is faster than anything you can do in JavaScript (except binary search/dichotomy if the array is appropriate). Note: it is a question of taste, but I would do a return false; at the end of your routine, to return a true Boolean...

Source Link
PhiLho
  • 41.3k
  • 6
  • 100
  • 136

It depends on your purpose. If you program for the Web, avoid indexOf, it isn't supported by IE6 (lot of them still used!), or do conditional use:

if (yourArray.indexOf !== undefined) result = yourArray.indexOf(target); else result = customSlowerSearch(yourArray, target); 

indexOf is probably coded in native code, so faster than anything you can do in JS (except binary search/dichotomy if the array is appropriate). Note: question of taste, but I would do a return false; at the end of your routine, to return a true boolean...