caching length

JavaScript performance comparison

Revision 66 of this test case created by testssss

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
cached
function computeStringMaxValue(string) {
  // Sanity check, returns -1 if we cannot continue
  if (typeof string !== 'string' || string.length < 2 || string > 500) {
    return -1;
  }

  var inputChars = string.toLowerCase().match(/[a-z]/g),
    charsUsageObj = {},
    sum = 0,
    i;

  if (!inputChars) { // We don't have valid letters in string
    return 0;
  }
 
  for (i = 0; i < inputChars.length; i++) {
    // Increment if we have char
    if (charsUsageObj.hasOwnProperty(inputChars[i])) {
      charsUsageObj[inputChars[i]]++;
    } else { // or add new record
      charsUsageObj[inputChars[i]] = 1;
    }
  }

  // Let's sort the array based on usage of each letter desc and join array
  var letterSorted = Object.keys(charsUsageObj).sort(function _sortFn(a, b) {
    if (charsUsageObj[a] < charsUsageObj[b]) {
      return -1;
    }

    if (charsUsageObj[a] > charsUsageObj[b]) {
      return 1;
    }

    return 0;
  });
 
  var alphabetOffset = 26 - letterSorted.length;
  for (i = 0; i < letterSorted.length; i++) {
    sum += (alphabetOffset + i + 1) * charsUsageObj[letterSorted[i]];
  }
 
  return sum;
}
computeStringMaxValue('We are looking forward for your entries...');
 
pending…
not cached
function computeStringMaxValue(string) {
  // Sanity check, returns -1 if we cannot continue
  if (typeof string !== 'string' || string.length < 2 || string > 500) {
    return -1;
  }

  var inputChars = string.toLowerCase(),
    alphabet = 'abcdefghijklmnopqrstuvwxyz',
    charsUsageObj = {},
    sum = 0;

  for (var i = 0; i < inputChars.length; i++) {
    var char = inputChars[i],
      isCharExists = charsUsageObj.hasOwnProperty(char),
      isCharInAlphabet = alphabet.indexOf(char) > -1;

    // Increment if we have char
    if (isCharExists) {
      charsUsageObj[char]++;
    } else if (isCharInAlphabet) { // or add new record
      charsUsageObj[char] = 1;
      alphabet = alphabet.replace(char, '');
    }
  }

  // Let's sort the array based on usage of each letter desc and join array
  var letterSorted = Object.keys(charsUsageObj).sort(function _sortFn(a, b) {
    if (charsUsageObj[a] < charsUsageObj[b]) {
      return -1;
    }

    if (charsUsageObj[a] > charsUsageObj[b]) {
      return 1;
    }

    return 0;
  }).join('');

  // Generate re-ordered alphaet, not used chars are with smaller weight
  alphabet = alphabet + letterSorted;

  // Compute the sum of the input string
  inputChars.split('').map(function _createNumArr(char) {
    var position = alphabet.indexOf(char);
    return (position > -1) ? (position + 1) : 0;
  }).map(function _sumFn(value) {
    sum += value;
  });
 
  return sum;
}
computeStringMaxValue('We are looking forward for your entries...');
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

Comment form temporarily disabled.

Add a comment