a

JavaScript performance comparison

Test case created

Preparation code

<script>
  function solve(input) {
   var result = [],
       counts = {},
       abs = Math.abs,
       
       
       
       
       
       // cached for better performance
       max, i, l, a;
 
   // if input is undefined or not an array
   // or everything is below or above 0, do nothing (there are no duplicates)
   if (!input || !input.length || input[0] >= 0 || input[input.length - 1] <= 0) {
    return result;
   }
 
   max = abs(input[0]); // store abs of first number (there is no need to go further than that)
   for (i = 0, l = input.length; i < l && input[i] <= max; i++) {
    a = abs(input[i]);
 
    if (counts[a] === true) {
     result[result.length] = a; // should be faster than push (in most browsers)
    } else {
     counts[a] = true;
    }
   }
 
   return result;
  }
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
a
solve([-4, -3, -1, 1, 2, 4, 67])
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment