Tweaking Some Loops

JavaScript performance comparison

Revision 5 of this test case created by Brian

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…

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