Proxy Trap Comparison

Benchmark created on


Setup

const identity = (raw) => raw

const parsers = {
  name(raw) {
    return raw.split(" ")
  },
  address(raw) {
    return raw.match(/(\d+)\s+(.*)/).slice(1, 3)
  },
}

const proxy = new Proxy(parsers, {
  get: (target, prop) => target[prop] || identity,
})

const source = JSON.stringify([
  {
    "name": "Ulick Olivi",
    "address": "5049 Holmberg Lane",
    "airport": "Uytash Airport"
  }, {
    "name": "Veronike Martello",
    "address": "5 Clarendon Road",
    "airport": "Cherepovets Airport"
  }, {
    "name": "Darrin Pearse",
    "address": "20537 Thompson Lane",
    "airport": "Tortolì Airport"
  }, {
    "name": "Doralynne Enochsson",
    "address": "73 Clyde Gallagher Parkway",
    "airport": "Mansons Landing Seaplane Base"
  }, {
    "name": "Scottie Glasner",
    "address": "8171 Glendale Alley",
    "airport": "A P Hill AAF (Fort A P Hill) Airport"
  }, {
    "name": "Nicolais Yeomans",
    "address": "0223 Manley Terrace",
    "airport": "Afore Airstrip"
  }, {
    "name": "Madeleine Gallant",
    "address": "16 Browning Center",
    "airport": "Sky Harbor Airport"
  }, {
    "name": "Morgana Larkkem",
    "address": "9 Fordem Point",
    "airport": "Aiome Airport"
  }, {
    "name": "Dillie Rankcom",
    "address": "6523 Lerdahl Road",
    "airport": "Kissidougou Airport"
  }, {
    "name": "Sherline Thombleson",
    "address": "4397 Utah Park",
    "airport": "Kaiapit Airport"
  },
])

const parseWithoutProxy = (raw) => JSON.parse(raw, (key, value) => key in parsers
  ? parsers[key](value)
  : value)

const parseWithProxy = (raw) => JSON.parse(raw, (key, value) => proxy[key](value))

Test runner

Ready to run.

Testing in
TestOps/sec
Parse no Proxy
parseWithoutProxy(source)
ready
Parse with Proxy
parseWithProxy(source)

ready

Revisions

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