String Search

Benchmark created on


Setup

class StringDB {
  constructor(items) {
    this.db = ''
    items.forEach((item, i) => {
      this.db += `"${i} ${item}"`
    })
  }
  
  search(query) {
    const matches = this.db.match(new RegExp(`(\\d+) ([^"]*${query}[^"]*)`, 'ig'))
    matches.forEach((m, i) => {
      const id = parseInt(m)
      matches[i] = [ id, m.replace(id + ' ', '') ]
    })
    return matches
  }
}

class MapDB {
  constructor(items) {
    this.db = items.map(item => item.toLowerCase())
  }
  
  search(query) {
    query = query.toLowerCase()
    return this.db.filter((item, i) => {
      return item.startsWith(query)
    })
  }
}

const items = [
  "None",
  "Home",
  "Settings",
  "Question",
  "Lock",
  "Unlock",
  "Power",
  "Arrow Right",
  "Arrow Up",
  "Arrow Left",
  "Arrow Down",
  "Login",
  "Logout",
  "User",
  "Group",
  "Forward",
  "Back",
  "Double Forward",
  "Double Back",
  "Bullet",
  "Bullet Right",
  "Bullet Up",
  "Bullet Left",
  "Bullet Down",
  "Bullet Chevron Right",
  "Bullet Chevron Up",
  "Bullet Chevron Left",
  "Bullet Chevron Down",
  "Bullet Plus",
  "Bullet Minus",
  "Bullet Info",
  "Bullet Question",
  "Bullet Exclamation",
  "Bullet Close",
  "Bullet Play",
  "Refresh",
  "Undo",
  "Sync",
  "Swap",
  "Rewind",
  "Reverse",
  "Step Backwards",
  "Stop",
  "Pause",
  "Play",
  "Record",
  "Step Forward",
  "Forward",
  "Fast Forward",
  "Hexagon",
  "Stop Sign",
  "Warning",
  "Pencil",
  "Trash",
  "Search",
  "Key",
  "Link",
  "Eye",
  "Marker",
  "Push Pin",
  "Tag",
  "Heart",
  "Paper Clip",
  "Sun",
  "Moon",
  "Cloud",
  "Lightning Bolt",
  "Umbrella",
  "Drop",
  "Star",
  "Earth",
  "Site Map",
  "Camera",
  "Picture",
  "Music Note",
  "Film",
  "Ticket",
  "Microphone",
  "Trophy",
  "Plane",
  "Rocket",
  "X",
  "Move",
  "Hand",
  "Comment",
  "Face Smile",
  "Face Neutral",
  "Face Frown",
  "Circle",
  "Circle Right",
  "Circle Up",
  "Circle Left",
  "Circle Down",
  "Circle Close",
  "Circle Play",
  "Circle Dot",
  "Circle Slash",
  "Circle Clock",
  "Caret Right",
  "Caret Up",
  "Caret Left",
  "Caret Down",
  "Caret Horizontal",
  "Caret Vertical",
  "Chevron Right",
  "Chevron Up",
  "Chevron Left",
  "Chevron Down"
]
const strDB = new StringDB(items)
const mapDB = new MapDB(items)

Test runner

Ready to run.

Testing in
TestOps/sec
arr search
mapDB.search('p')
mapDB.search('pa')
mapDB.search('pap')
mapDB.search('pape')
mapDB.search('paper')
mapDB.search('paper ')
mapDB.search('paper c')
mapDB.search('paper cl')
mapDB.search('paper cli')
mapDB.search('paper clip')
ready
regex search
strDB.search('p')
strDB.search('pa')
strDB.search('pap')
strDB.search('pape')
strDB.search('paper')
strDB.search('paper ')
strDB.search('paper c')
strDB.search('paper cl')
strDB.search('paper cli')
strDB.search('paper clip')
ready

Revisions

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