Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
storage test 2
(version: 1)
Comparing performance of:
class vs function vs object
Created:
6 years ago
by:
Registered User
Jump to the latest result
Tests:
class
class Storage { getStoredData () { return JSON.parse(window.localStorage.getItem('TF1player') || '{}') } get (key) { this.data = this.data || this.getStoredData() return this.data[key] } set (key, value) { this.data = this.data || this.getStoredData() this.data[key] = value window.localStorage.setItem('TF1player', JSON.stringify(this.data)) } clearStoredData () { this.data = undefined window.localStorage.clear() } } const storageModule = () => { const state = {} const setState = newState => Object.keys(newState).forEach(key => (state[key] = newState[key])) const getStoredData = () => JSON.parse(window.localStorage.getItem('TF1player') || '{}') const get = key => { const { data } = state setState({ data: data || getStoredData() }) return state.data[key] } const set = (key, value) => { const { data = getStoredData() } = state const newData = { ...data, [key]: value } setState({ data: newData }) window.localStorage.setItem('TF1player', JSON.stringify(newData)) } const clearStoredData = () => { setState({ data: undefined }) window.localStorage.clear() } return { get, set, clearStoredData, } } const Storage2 = { data: undefined, setData: function () { this.data = this.data || this.getStoredData() }, getStoredData: function () { return JSON.parse(window.localStorage.getItem('TF1player') || '{}') }, get: function (key) { this.setData() return this.data[key] }, set: function (key, value) { this.setData() this.data[key] = value window.localStorage.setItem('TF1player', JSON.stringify(this.data)) }, clearStoredData: function () { this.data = undefined window.localStorage.clear() }, } const storageClass = new Storage() const storageFunction = storageModule() const storageObject = Storage2 storageClass.get('test1', 'test value 1')
function
class Storage { getStoredData () { return JSON.parse(window.localStorage.getItem('TF1player') || '{}') } get (key) { this.data = this.data || this.getStoredData() return this.data[key] } set (key, value) { this.data = this.data || this.getStoredData() this.data[key] = value window.localStorage.setItem('TF1player', JSON.stringify(this.data)) } clearStoredData () { this.data = undefined window.localStorage.clear() } } const storageModule = () => { const state = {} const setState = newState => Object.keys(newState).forEach(key => (state[key] = newState[key])) const getStoredData = () => JSON.parse(window.localStorage.getItem('TF1player') || '{}') const get = key => { const { data } = state setState({ data: data || getStoredData() }) return state.data[key] } const set = (key, value) => { const { data = getStoredData() } = state const newData = { ...data, [key]: value } setState({ data: newData }) window.localStorage.setItem('TF1player', JSON.stringify(newData)) } const clearStoredData = () => { setState({ data: undefined }) window.localStorage.clear() } return { get, set, clearStoredData, } } const Storage2 = { data: undefined, setData: function () { this.data = this.data || this.getStoredData() }, getStoredData: function () { return JSON.parse(window.localStorage.getItem('TF1player') || '{}') }, get: function (key) { this.setData() return this.data[key] }, set: function (key, value) { this.setData() this.data[key] = value window.localStorage.setItem('TF1player', JSON.stringify(this.data)) }, clearStoredData: function () { this.data = undefined window.localStorage.clear() }, } const storageClass = new Storage() const storageFunction = storageModule() const storageObject = Storage2 storageFunction.get('test1', 'test value 1')
object
class Storage { getStoredData () { return JSON.parse(window.localStorage.getItem('TF1player') || '{}') } get (key) { this.data = this.data || this.getStoredData() return this.data[key] } set (key, value) { this.data = this.data || this.getStoredData() this.data[key] = value window.localStorage.setItem('TF1player', JSON.stringify(this.data)) } clearStoredData () { this.data = undefined window.localStorage.clear() } } const storageModule = () => { const state = {} const setState = newState => Object.keys(newState).forEach(key => (state[key] = newState[key])) const getStoredData = () => JSON.parse(window.localStorage.getItem('TF1player') || '{}') const get = key => { const { data } = state setState({ data: data || getStoredData() }) return state.data[key] } const set = (key, value) => { const { data = getStoredData() } = state const newData = { ...data, [key]: value } setState({ data: newData }) window.localStorage.setItem('TF1player', JSON.stringify(newData)) } const clearStoredData = () => { setState({ data: undefined }) window.localStorage.clear() } return { get, set, clearStoredData, } } const Storage2 = { data: undefined, setData: function () { this.data = this.data || this.getStoredData() }, getStoredData: function () { return JSON.parse(window.localStorage.getItem('TF1player') || '{}') }, get: function (key) { this.setData() return this.data[key] }, set: function (key, value) { this.setData() this.data[key] = value window.localStorage.setItem('TF1player', JSON.stringify(this.data)) }, clearStoredData: function () { this.data = undefined window.localStorage.clear() }, } const storageClass = new Storage() const storageFunction = storageModule() const storageObject = Storage2 storageObject.get('test1', 'test value 1')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
class
function
object
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Based on the benchmark results, here are my observations: **Benchmarks for Function:** * Executions per second (EPS): 185978.96875 * The Chrome 79 browser performs well with an EPS of over 185K. **Benchmarks for Object:** * Executions per second (EPS): 184536.828125 * The Chrome 79 browser still performs relatively well, but slightly slower than the function benchmark. **Benchmarks for Class:** * Executions per second (EPS): 103211.7265625 * The Chrome 79 browser is significantly slower with an EPS of around 100K. In general, it appears that: 1. Function-based implementation performs well. 2. Object-based implementation performs relatively poorly compared to the function-based implementation. 3. Class-based implementation performs even poorer than the object-based implementation. However, without more context and additional benchmark results, it's difficult to provide a definitive conclusion or recommendations for improving performance in these implementations.
Related benchmarks:
Local storage cahce
Local storage cahce1
Local storage cahce2
Queue data structure
Map vs Object: Lookup, insert and delete
Comments
Confirm delete:
Do you really want to delete benchmark?