calc memory size with switch,if,arrays,pow

JavaScript performance comparison

Revision 10 of this test case created by tomByrer

Info

Revision 10: added name only; I didn't edit anything else. Clearly tests were added far beyond the original switch vs if.

Preparation code

<script>
  function sizeToBytesSwitch(size, format) {
    switch (format) {
    case 'MB':
      return size * 1048576;
    case 'GB':
      return size * 1073741824;
    case 'TB':
      return size * 1099511627776;
    default:
      return size * 1024;
    }

  }

  function sizeToBytesObject(amount, format) {
    var sizes = {
      'MB': 1048576,
      'GB': 1073741824,
      'TB': 1099511627776
    }

    if (sizes[format]) {
      return amount * sizes[format];
    }

    return amount * 1024;
  }
  var sizes = {
    'MB': 1048576,
    'GB': 1073741824,
    'TB': 1099511627776
  }

  function sizeToBytesObjectOnce(amount, format) {
    if (sizes[format]) {
      return amount * sizes[format];
    }

    return amount * 1024;
  }
  var sizes2 = {
    'KB': 1024,
    'MB': 1048576,
    'GB': 1073741824,
    'TB': 1099511627776
  }

  function sizeToBytesObjectOncePositive(amount, format) {
    if (sizes2[format]) {
      return amount * sizes2[format];
    }

    return amount * 1024;
  }

  function sizeToBytesVariables(size, format) {
    var KBYTES_IN_BYTES = 1024,
        MEGABYTE_IN_BYTES = 1048576,
        GIGABYTE_IN_BYTES = 1073741824,
        TERABYTE_IN_BYTES = 1099511627776;

    switch (format) {
    case 'MB':
      return size * MEGABYTE_IN_BYTES;
    case 'GB':
      return size * GIGABYTE_IN_BYTES;
    case 'TB':
      return size * TERABYTE_IN_BYTES;
    default:
      return size * KBYTES_IN_BYTES;
    }

  }


  var sizeToBytesClosure = (function() {

    'use strict';

    var bytesIn = {
      'KB': 1024,
      'MB': 1048576,
      'GB': 1073741824,
      'TB': 1099511627776
    };

    return function(amount, format) {

      if (bytesIn[format]) {

        return amount * bytesIn[format];

      }

      return amount * bytesIn.KB;

    };

  }());

  var sizeToBytesClosure2 = (function() {

    'use strict';

    var bytesIn = {
      'KB': 1024,
      'MB': 1048576,
      'GB': 1073741824,
      'TB': 1099511627776
    };


    return function(amount, format) {

      var bytesInSelectedFormat = bytesIn[format];

      if (bytesInSelectedFormat) {

        return amount * bytesInSelectedFormat;

      }

      return amount * bytesIn.KB;

    };

  }());

  var sizeToBytesClosure3 = (function() {

    'use strict';

    var bytesIn = {
      'KB': 1024,
      'MB': 1048576,
      'GB': 1073741824,
      'TB': 1099511627776
    };


    return function(amount, format) {
      return amount * (bytesIn[format] || bytesIn.KB);
    };

  }());

  var sizeToBytesClosure4 = (function() {

    'use strict';

    var sizes = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

    return function(amount, format) {
      return amount * Math.pow(1024, sizes.indexOf(format) + 1);
    };

  }());

var sizeToBytesClosure5 = (function () {

        'use strict';

        var base = 1024,
                sizes = {
                        'KB': Math.pow(base, 1),
                        'MB': Math.pow(base, 2),
                        'GB': Math.pow(base, 3),
                        'TB': Math.pow(base, 4),
                        'PB': Math.pow(base, 5),
                        'EB': Math.pow(base, 6),
                        'ZB': Math.pow(base, 7),
                        'YB': Math.pow(base, 8)
                };

        return function (amount, format) {
                return amount * sizes[format];
        };

}());
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Switch
for (var i = 7; i < 1000000; i *= 13) {
  var test = sizeToBytesSwitch(i, 'KB');
}
pending…
Object lookup
for (var i = 7; i < 1000000; i *= 13) {
  var test = sizeToBytesObject(i, 'KB');
}
pending…
Switch (with variables)
for (var i = 7; i < 1000000; i *= 13) {
  var test = sizeToBytesVariables(i, 'KB');
}
pending…
Object lookup hoisted
for (var i = 7; i < 1000000; i *= 13) {
  var test = sizeToBytesObjectOnce(i, 'KB');
}
pending…
Positive object lookup hoisted
for (var i = 7; i < 1000000; i *= 13) {
  var test = sizeToBytesObjectOncePositive(i, 'KB');
}
pending…
Positive object lookup hoisted - Closure
for (var i = 7; i < 1000000; i *= 13) {
  var test = sizeToBytesClosure(i, 'KB');
}
pending…
Positive object lookup hoisted - Closure (Optimized)
for (var i = 7; i < 1000000; i *= 13) {
  var test = sizeToBytesClosure2(i, 'KB');
}
pending…
Shorthand, no local variables
for (var i = 7; i < 1000000; i *= 13) {
  var test = sizeToBytesClosure3(i, 'KB');
}
pending…
Using Math.pow()
for (var i = 7; i < 1000000; i *= 13) {
  var test = sizeToBytesClosure4(i, 'KB');
}
pending…
Using Math.pow() - moved calculation to var
for (var i = 7; i < 1000000; i *= 13) {
  var test = sizeToBytesClosure5(i, 'KB');
}
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