Regex vs Array

Edit on GitHub

Array v regex search

Setup

name is a string

Tests

  • Regex

    function regexTest (name) {
      if (new RegExp('mean|percentpass|percentunder30s').test(name)) {
        return 'latest'
      }
    
      return 'sum'
    }
    regexTest('percentunder30s')
  • Array

    function arrayTest (name) {
      if (['mean', 'percentpass', 'percentunder30s'].indexOf(name) !== -1) {
        return 'latest'
      }
    
      return 'sum'
    }
    
    arrayTest('percentunder30s')