Tweaking Some Loops

JavaScript performance comparison

Revision 6 of this test case created

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
function bitmap(opt_input) {
  var base64 =
      'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
      'abcdefghijklmnopqrstuvwxyz' +
      '0123456789-_';

  var result = [];
  for (var i = 0; i < 200; i++) {
    var ch = opt_input && opt_input.charAt(Math.floor(i / 6)) || 'A';
    result[i] = (1 << (i % 6)) & base64.indexOf(ch) ? 1 : 0;
  }
  return result;
};

bitmap('');
bitmap('A');
bitmap('ASDQWEQFQWEQW56_-as89');
bitmap('AAAAAAAAAAAAAAAAAAAAAAAAAAB');
pending…
B
function bitmap(opt_input) {
  var result = [];
  for (var i = 0; i < 200; i += 6) {
    var code = opt_input && opt_input.charCodeAt(Math.floor(i / 6)) || 65;
    var base = 0;
    // A - Z
    if (code >= 65 && code <= 90) {
      base = code - 65;
    // a - z
    } else if (code >= 97 && code <= 122) {
      base = code - 97 + 26;
    // _
    } else if (code == 95) {
      base = 63;
    // 0 - 9
    } else if (code >= 48) {
      base = code - 48 + (2 * 26);
    // -
    } else {
      base = 62;
    }
    for (var j = 0; (j < 6) && (i + j < 200); j++) {
      result[i + j] = (1 << j) & base ? 1 : 0;
    }
  }
  return result;
};

bitmap('');
bitmap('A');
bitmap('ASDQWEQFQWEQW56_-as89');
bitmap('AAAAAAAAAAAAAAAAAAAAAAAAAAB');
pending…
C
function bitmap(opt_input) {
  var result = [];
  for (var i = 0; i < 200; i += 6) {
    var code = opt_input && opt_input.charCodeAt(Math.floor(i / 6)) || 65;
    var base = 0;
    // A - Z
    if (code >= 65 && code <= 90) {
      base = code - 65;
    // a - z
    } else if (code >= 97 && code <= 122) {
      base = code - 97 + 26;
    // _
    } else if (code == 95) {
      base = 63;
    // 0 - 9
    } else if (code >= 48) {
      base = code - 48 + (2 * 26);
    // -
    } else {
      base = 62;
    }
    result[i] = 1 & base ? 1 : 0;
    result[i + 1] = 2 & base ? 1 : 0;
    result[i + 2] = 4 & base ? 1 : 0;
    result[i + 3] = 8 & base ? 1 : 0;
    result[i + 4] = 16 & base ? 1 : 0;
    result[i + 5] = 32 & base ? 1: 0;
  }
  return result;
};

bitmap('');
bitmap('A');
bitmap('ASDQWEQFQWEQW56_-as89');
bitmap('AAAAAAAAAAAAAAAAAAAAAAAAAAB');
pending…
C w/o floor
function bitmap(opt_input) {
  var result = [];
  for (var i = 0, k = 0; i < 200; i += 6, k++) {
    var code = opt_input && opt_input.charCodeAt(k) || 65;
    var base = 0;
    // A - Z
    if (code >= 65 && code <= 90) {
      base = code - 65;
    // a - z
    } else if (code >= 97 && code <= 122) {
      base = code - 97 + 26;
    // _
    } else if (code == 95) {
      base = 63;
    // 0 - 9
    } else if (code >= 48) {
      base = code - 48 + (2 * 26);
    // -
    } else {
      base = 62;
    }
    result[i] = 1 & base ? 1 : 0;
    result[i + 1] = 2 & base ? 1 : 0;
    result[i + 2] = 4 & base ? 1 : 0;
    result[i + 3] = 8 & base ? 1 : 0;
    result[i + 4] = 16 & base ? 1 : 0;
    result[i + 5] = 32 & base ? 1: 0;
  }
  return result;
};

bitmap('');
bitmap('A');
bitmap('ASDQWEQFQWEQW56_-as89');
bitmap('AAAAAAAAAAAAAAAAAAAAAAAAAAB');
pending…
D
function bitmap(opt_input) {
  var result = [];
  if (!opt_input) {
    return result;
  }
  for (var i = 0, k = 0; (k < opt_input.length) && (i < 200);
       i += 6, k++) {
    var code = opt_input && opt_input.charCodeAt(k) || 65;
    // A = noop
    if (code == 65) {
      continue;
    }
    var base = 0;
    // B - Z
    if (code > 65 && code <= 90) {
      base = code - 65;
    // a - z
    } else if (code >= 97 && code <= 122) {
      base = code - 97 + 26;
    // _
    } else if (code == 95) {
      base = 63;
    // 0 - 9
    } else if (code >= 48) {
      base = code - 48 + (2 * 26);
    // -
    } else {
      base = 62;
    }
    result[i] = base & 1;
    result[i + 1] = base & 2;
    result[i + 2] = base & 4;
    result[i + 3] = base & 8;
    result[i + 4] = base & 16;
    result[i + 5] = base & 32;
  }
  return result;
};

bitmap('');
bitmap('A');
bitmap('ASDQWEQFQWEQW56_-as89');
bitmap('AAAAAAAAAAAAAAAAAAAAAAAAAAB');
pending…
E
function bitmap(opt_input) {
  var result = [];
  if (!opt_input) {
    return result;
  }
  for (var i = 0, k = 0; (k < opt_input.length) && (i < 200);
       i += 6, k++) {
    var code = opt_input && opt_input.charCodeAt(k) || 65;
    // A = noop
    if (code == 65) {
      continue;
    }
    var base = 0;
    // B - Z
    if (code > 65 && code <= 90) {
      base = code - 65;
    // a - z
    } else if (code >= 97 && code <= 122) {
      base = code - 97 + 26;
    // _
    } else if (code == 95) {
      base = 63;
    // 0 - 9
    } else if (code >= 48) {
      base = code - 48 + (2 * 26);
    // -
    } else {
      base = 62;
    }
    if (base & 1) result[i] = 1;
    if (base & 2) result[i + 1] = 1;
    if (base & 4) result[i + 2] = 1;
    if (base & 8) result[i + 3] = 1;
    if (base & 16) result[i + 4] = 1;
    if (base & 32) result[i + 5] = 1;    
  }
  return result;
};

bitmap('');
bitmap('A');
bitmap('ASDQWEQFQWEQW56_-as89');
bitmap('AAAAAAAAAAAAAAAAAAAAAAAAAAB');
pending…
F
function bitmap(opt_input) {
  var result = [];
  if (!opt_input) {
    return result;
  }
  for (var i = 0, k = 0; (k < opt_input.length) && (i < 200);
       i += 6, k++) {
    var code = opt_input && opt_input.charCodeAt(k) || 65;
    // A = noop
    if (code == 65) {
      continue;
    }
    var base = lookup[code]
    result[i] = base & 1;
    result[i + 1] = base & 2;
    result[i + 2] = base & 4;
    result[i + 3] = base & 8;
    result[i + 4] = base & 16;
    result[i + 5] = base & 32;
  }
  return result;
};

var lookup = [62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 95, 96, 97, 98, 63, 100, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
bitmap('');
bitmap('A');
bitmap('ASDQWEQFQWEQW56_-as89');
bitmap('AAAAAAAAAAAAAAAAAAAAAAAAAAB');
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