Tweaking Some Loops

JavaScript performance comparison

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…

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