Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of JSON Parse/Stringify

I want to store a maximum of around 10 thousand integers in sessionStorage. I will need to JSON parse and stringify to update this array of integers.

Is this a terrible idea? Or is the performance hit not too bad?

like image 980
Tim Morris Avatar asked Jun 22 '18 16:06

Tim Morris


Video Answer


2 Answers

You shouldn't use SessionStorage for that purpose because it is blocking main thread that can leads to hanging your application.

Check IndexedDb instead

It designed to be async and more-less fast. And also it has pretty nice support:

enter image description here https://caniuse.com/#search=indexeddb

Hope this helps

like image 174
Drag13 Avatar answered Sep 18 '22 12:09

Drag13


As @IrkenInvader said test and measure on your reference browser(eg. on low end mobile device parsing can be much slower).

A quick test:

function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}
var numbers = Array.apply(null, {length: 10000}).map(Function.call, (x) => getRandomInt(100));
var start = window.performance.now();
window.sessionStorage.setItem('test', JSON.stringify(numbers));
var end = window.performance.now();
console.log("timing", end-start);
like image 20
filippo Avatar answered Sep 16 '22 12:09

filippo