Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9009217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:07:58+00:00 2026-06-16T02:07:58+00:00

I am trying to boost my Javascript understanding, so I’ve been looking through the

  • 0

I am trying to boost my Javascript understanding, so I’ve been looking through the Zepto library. I came across this line:

uniq = function(array){
    return array.filter(function(item, idx){
        return array.indexOf(item) == idx
    })
}

What is the purpose of this function? From what I can tell, it is creating a new, unique array of elements, right? But isn’t it essentially just cloning the array? If so, wouldn’t array.slice() be faster?

Finally, wouldn’t it increase performance to change array.indexOf(item) to array.indexOf(item,idx)? Or better yet, just return true? When does array.indexOf(item)==idx not equal true? Is this to prevent duplicate items? But when would that ever actually happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:07:59+00:00Added an answer on June 16, 2026 at 2:07 am

    it is creating a new, unique array of elements, right?

    It just filter your array elements to return unique elements.

    demo

    But isn't it essentially just cloning the array?

    No as I explain above.

    If so, wouldn't array.slice() be faster?

    Slice doesn’t remove duplicates.

    Finally, wouldn't it increase performance to change array.indexOf(item) to array.indexOf(item,idx)? Or better yet, just return true?

    If you only return true you won’t identify if the element is duplicated or not.

    demo

    When does array.indexOf(item)==idx not equal true?

    Example:
    I have the following array:

    ['10', '20', '30', '20', '10']

    Iterations:

    • 1: array.IndexOf(10) == 0 ? // yes, so return true
    • 2: array.IndexOf(20) == 1 ? // yes, so return true
    • 3: array.IndexOf(30) == 2 ? // yes, so return true
    • 4: array.IndexOf(20) == 3 ? // no because array.indexOf(20) is 1 , so return false
    • 5: array.IndexOf(10) == 4 ? // no because array.indexOf(10) is 2 , so return false

    So, when the element has already been found it gets false because the indexes are not the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I'm trying to use boost.process library. I downloaded package from this location ,
I am trying to use Boost.Spirit (V. 2.5) library to create a mini-calculator. Features
I am trying to use boost::bind with a boost::function using this. It seems a
I'm trying to use boost::iterator_facade with an incomplete Value template argument. This fails, because
I'm new to boost and I've been trying out boost::asio. The problem is I
I am trying to do something like this: BOOST_FOREACH (const std::string& line, allLinesOf(someFileLoadedIntoString)) {
Ive currently been messing around with boost trying it out. When i try to
I'm trying to use boost.signals library. I know that to use it, first I
I'm trying to use boost unit testing alongside the Allegro graphics library, but both
I am trying to install boost 1.5 into android according to this . When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.