Review compatibility issues and performance optimization from the JavaScript Array (from yuber blog)

Source: Internet
Author: User
Tags javascript array

Review compatibility issues and performance optimization from the JavaScript Array (from yuber blog)
Reason

JavaScript array deduplication is often used in front-end pen questions, such:

There are arrays var arr = ['A', 'B', 'C', '1', 0, 'C', 1, '', 1, 0], use JavaScript to implement the unqiue function so that unique (arr) Returns ['A', 'B', 'C', '1', 0, 1, '']

As a pen exam, there are two exam points:

  1. Correct. Don't underestimate this test point. Considering that JavaScript often runs on a browser, it is not easy to ensure the correctness of a function in a variety of browser environments, do not believe that you will continue reading this blog.

  2. Performance. Although the JavaScript language itself (in a narrow sense, does not contain extensions such as DOM) in most cases will not cause performance problems, it is unfortunately a question, so the interviewer will still take performance as a test site.

    Before proceeding, we recommend that you implement a version that you think is the best.

    Intuitive solution

    For array deduplication, as long as the program is written, the first solution can be obtained immediately:

    function unique(arr) {  var ret = []  for (var i = 0; i < arr.length; i++) {    var item = arr[i]    if (ret.indexOf(item) === -1) {      ret.push(item)    }  }  return ret}
      Code laycode-v1.1

      Intuition is often very reliable. In modern browsers, the above function is very correct and the performance is good. However, the biggest sorrow of the front-end is the challenge of supporting various runtime environments. The indexOf method of the array does not exist in the IE6-8. The intuitive solution should be slightly transformed:

      var indexOf = [].indexOf ?    function(arr, item) {      return arr.indexOf(item)    } :    function indexOf(arr, item) {      for (var i = 0; i < arr.length; i++) {        if (arr[i] === item) {          return i        }      }      return -1    }function unique(arr) {  var ret = []  for (var i = 0; i < arr.length; i++) {    var item = arr[i]    if (indexOf(ret, item) === -1) {      ret.push(item)    }  }  return ret}
      1. 1
      2. 2
      3. 3
      4. 4
      5. 5
      6. 6
      7. 7
      8. 8
      9. 9
      10. 10
      11. 11
      12. 12
      13. 13
      14. 14
      15. 15
      16. 16
      17. 17
      18. 18
      19. 19
      20. 20
      21. 21
      22. 22
      23. 23
      24. 24
      25. 25
      26. 26
      27. 27
      28. 28
      29. 29
      Code laycode-v1.1

      This step is correct, but in terms of performance, the Dual Loop will make the interviewer uncomfortable.

      Optimization Scheme

      When it comes to optimization, there are always a lot of flowers and flowers. But the Eight Immortals are often not grounded, and Baihua is easy to attract bugs. The optimization schemes for Array deduplication are not discussed here. The following describes only one of the most commonly used methods with good results.

      function unique(arr) {  var ret = []  var hash = {}  for (var i = 0; i < arr.length; i++) {    var item = arr[i]    var key = typeof(item) + item    if (hash[key] !== 1) {      ret.push(item)      hash[key] = 1    }  }  return ret}
        Code laycode-v1.1

        The core is to build a hash object to replace indexOf. note that in JavaScript, the key value of an object can only be a string. Therefore, var key = typeof (item) + item must be used to distinguish between value 1 and string '1.

         

      Related Article

      Contact Us

      The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

      If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

      A Free Trial That Lets You Build Big!

      Start building with 50+ products and up to 12 months usage for Elastic Compute Service

      • Sales Support

        1 on 1 presale consultation

      • After-Sales Support

        24/7 Technical Support 6 Free Tickets per Quarter Faster Response

      • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.