parseInt vs lookup for reversing

Benchmark created by Phillip H on


Setup

var s = 'This is a test. 1900-12-32 -- 1200-02-10';
    var pattern = /[0-9]/g;
    var map = {'0':'9', '1':'8', '2':'7', '3':'6', '4':'5', '5':'4', '6':'3', '7':'2', '8':'1', '9':'0'};
    var map_func = function(m) { return map[m]; };
    var parseint_func = function(m) { return '' + (9 - parseInt(m)); };

Test runner

Ready to run.

Testing in
TestOps/sec
parseInt
var result = s.replace(pattern, parseint_func);
ready
mapping
var result = s.replace(pattern, map_func);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Phillip H on