String uniqueness

JavaScript performance comparison

Test case created by Gady

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let arr = [
  '2a	D0CAr^!wWqEoZv7e\\ ',
  'Ciz+K\'WwmZN]b2rs.	tBG!fsj',
  'Y+k@r?R\r_jKB9~VM^tvIGm/2\\, ',
  '|3W6*>K/M~NlIs8G{[A,FpadmCV U\\w#,r1^? '
  ]

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Set
function hasUniqueChars(str){
  return new Set(str).size === str.length;
}

arr.forEach(str => {
  hasUniqueChars(str);
});
pending…
For
function hasUniqueChars(str){
  let arr = Array.from(str);
  let map = {};
  for (let i = 0; i < str.length; i++) {
    if(map[arr[i]]) return false;
    map[arr[i]] = true;
  }
  return true;
}

arr.forEach(str => {
  hasUniqueChars(str);
});
pending…
Regex
function hasUniqueChars(str) {
  return !/(.).*\1/.test(str);
}

arr.forEach(str => {
  hasUniqueChars(str);
});
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments