Skip to content

Map is now faster than object (1.5x get, 2.6x set)#21

Open
davidmarkclements wants to merge 3 commits into
masterfrom
map
Open

Map is now faster than object (1.5x get, 2.6x set)#21
davidmarkclements wants to merge 3 commits into
masterfrom
map

Conversation

@davidmarkclements

Copy link
Copy Markdown
Collaborator

Using Map is now significantly faster than using objects, and this has an added advantage of using objects for keys

bench output for this branch:

GET { mean: 54075,
  stdev: 19334.373176059024,
  count: 100,
  sum: 5407500,
  sqsum: 329792361111.1111 }
SET { mean: 14783.333333333345,
  stdev: 1576.1155451425414,
  count: 100,
  sum: 1478333.3333333344,
  sqsum: 22103108465.608475 }

bench output for master branch

GET { mean: 35880.15873015876,
  stdev: 10512.551706709608,
  count: 100,
  sum: 3588015.873015876,
  sqsum: 139789953388.76306 }
SET { mean: 5580.721850837276,
  stdev: 475.5864362080366,
  count: 100,
  sum: 558072.1850837276,
  sqsum: 3137063883.4717693 }

@davidmarkclements

Copy link
Copy Markdown
Collaborator Author

one way this could break user code, is if someone is doing this:

lru.set(1, 'foo')
lru.get('1')

That will work with objects, but not maps. We can either protect against by converting numbers to strings (might have a perf penalty) or we can release as major

@dominictarr

Copy link
Copy Markdown
Owner

looks good. If in doubt, just release as major. I don't think numbers as strings for keys should be explicitly supported.

@arj03

arj03 commented Sep 21, 2020

Copy link
Copy Markdown

Nice find @davidmarkclements

@gajus

gajus commented May 19, 2021

Copy link
Copy Markdown

For what it is worth, @sindresorhus implements equivalent logic in https://www.npmjs.com/package/quick-lru

@dcastil

dcastil commented Jun 26, 2022

Copy link
Copy Markdown

Would love to see this in a future release. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants