Skip to content

Instantly share code, notes, and snippets.

@aburd
Created October 12, 2019 16:22
Show Gist options
  • Save aburd/31c84f0a3db5e392e182c59d02c5ea1b to your computer and use it in GitHub Desktop.
Save aburd/31c84f0a3db5e392e182c59d02c5ea1b to your computer and use it in GitHub Desktop.

Revisions

  1. aburd created this gist Oct 12, 2019.
    33 changes: 33 additions & 0 deletions js_object_map_get_set_comparison.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    const testKeyName = 'foo'
    const testAmount = 100000000
    const obj = { [testKeyName]: 'bar' }
    const map = new Map()
    map.set(testKeyName, 'bar')

    let objTestName = `Reading a property from obj ${testAmount} times`
    console.time(objTestName)
    for (let i = 0; i < testAmount; i++) {
    obj[testKeyName]
    }
    console.timeEnd(objTestName)

    let mapTestName = `Reading a property from map ${testAmount} times`
    console.time(mapTestName)
    for (let i = 0; i < testAmount; i++) {
    map.get(testKeyName)
    }
    console.timeEnd(mapTestName)

    objTestName = `Writing a property to obj ${testAmount} times`
    console.time(objTestName)
    for (let i = 0; i < testAmount; i++) {
    obj[testKeyName] = i
    }
    console.timeEnd(objTestName)

    mapTestName = `Writing a property to map ${testAmount} times`
    console.time(mapTestName)
    for (let i = 0; i < testAmount; i++) {
    map.set(testKeyName, i)
    }
    console.timeEnd(mapTestName)