Creating a local storage entry

We can add entries to local storage in a more intuitive and convenient way than cookies. Here's what the syntax looks like using localStorage.setItem(key, value):

localStorage.setItem('myKey', 'awesome value');console.log('entry added');
The localStorage is a synchronous API. It'll block the thread execution until completed.

Let's now quickly, and roughly, determine how much time on average localStorage.setItem takes, as follows:

const now = performance.now();for(let i=0;i<1000;i++) {    localStorage.setItem(`myKey${i}`, `myValue${i}`);}const then = performance.now();console.log('Done')console.log(`Time taken: ${(then - now)/1000} milliseconds per operation`);

The result, as you can see, is not that bad:

So, it ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.