Remove Formatting asdfsdf

Benchmark created by sean on


Setup

isFormattedNegative(field) {
        if (field !== undefined && field.toString().substring(0, 1) === "(") {
          return true;
        }
        if (field !== undefined && field.toString().substring(0, 1) === "-" && field !== "-") {
          return true;
        }
        return false;
      }
    
    var base = [
    
      '$45,000',
      '$567,456,778',
      '456.65%',
      '(567)'
    
    ];
    
    var list = [];
    var j = 0;
    for (var i = 0; i < base.length * 20000; i++) {
        list.push(base[j]);
        j += 1;
        j = j % base.length;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Old
function oldRemove(field) {
    if (!field) return 0;
    var isNegative = isFormattedNegative(field);
    field = parseFloat(field.toString().replace(/,|%|\$|\(|\)/g, ''));
    if (isNegative && field.toString().substring(0, 1) !== "-") field = -field;
    if (isNaN(field)) field = 0;
    return field;
}

var len = list.length;
for (var i = 0; i < len; i++) {
    oldRemove(list[i]);
}
ready
New
function newRemove() {
    if (!field) {
      return 0;
    }

    let isNegative = this.isFormattedNegative(field);

    if (typeof field !== 'number') {
      field = parseFloat(field.toString().replace(/,|%|\$|\(|\)/g, ''));
    }

    if (isNegative && field > 0) {
      field = -field;
    }
    if (isNaN(field)) {
      field = 0;
    }
    return field;
}


var len = list.length;
for (var i = 0; i < len; i++) {
    newRemove(list[i]);
}
ready

Revisions

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