localStorage.setItem('foo', 'bar')
sessionStorage.setItem('foo', 'bar')
var ARR = ['bar'];
var FOO = localStorage.getItem('foo');
var FOO = sessionStorage.getItem('foo');
var FOO = ARR[Math.floor(Math.random() * ARR.length)]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
localStorage | |
sessionStorage | |
Read Array |
Test name | Executions per second |
---|---|
localStorage | 2459354.8 Ops/sec |
sessionStorage | 2475988.0 Ops/sec |
Read Array | 2722639.2 Ops/sec |
Let's dive into the world of MeasureThat.net.
What is being tested?
MeasureThat.net provides a platform for users to create and run JavaScript microbenchmarks, which are small scripts that measure the execution time of specific pieces of code. In this case, we have three test cases:
localStorage
: This test case measures the time it takes to retrieve data from localStorage
.sessionStorage
: Similar to the first one, but for sessionStorage
, which is another type of local storage that can only be accessed during a user's session.Read Array
: This test case measures the time it takes to access an array using indexing (e.g., arr[0]
) or randomness (e.g., Math.random() * arr.length
). The purpose is to compare the performance of accessing an array versus retrieving data from local storage.Options compared
The options being compared are:
localStorage
vs. a hardcoded arraysessionStorage
These two options are being compared because they serve different purposes and have different characteristics:
localStorage
: Data is stored locally on the client-side, and it persists across page reloads and user sessions.Pros and Cons
Here are some pros and cons of each approach:
localStorage
:Library
There isn't a specific library being used in this benchmark. However, the use of localStorage
and sessionStorage
implies that the browser's built-in local storage API is being utilized.
Special JS feature or syntax
None are mentioned specifically for this benchmark.
Other alternatives
If you wanted to modify this benchmark, you could consider adding additional test cases, such as:
sessionStorage
with different access patterns (e.g., accessing a specific key multiple times)IndexedDB
)arr[0]
, arr[Math.floor(Math.random() * arr.length)]
)Keep in mind that these modifications would require updating the benchmark definition and script preparation code to accommodate the new test cases.