Created
February 29, 2016 19:46
-
-
Save anaisbetts/d028545eff560a479543 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Observable, Subject} from 'rx'; | |
import _ from 'lodash'; | |
export default class TimedLocalStorage { | |
constructor(originalLocalStorage) { | |
this.inner = originalLocalStorage; | |
this.tick = new Subject(); | |
this.count = 0; | |
this.elapsed = 0; | |
const toOverride = ['key', 'getItem', 'setItem', 'removeItem', 'clear']; | |
_.each(toOverride, (k) => { | |
let item = k; | |
this[item] = (...args) => { | |
let s = performance.now(); | |
let ret = this.inner[item](...args); | |
this.tick.onNext(performance.now() - s); | |
return ret; | |
}; | |
}); | |
this.tick | |
.do((elapsed) => { | |
this.start = this.start || performance.now(); | |
this.count++; | |
this.elapsed += elapsed; | |
}) | |
.throttle(1000) | |
.subscribe(() => { | |
let total = performance.now() - this.start - 1000; | |
if (total < 0) total += 1000; | |
let percentage = this.elapsed / total * 100; | |
if (total > 50) { | |
console.log(`Local Storage accounted for ${this.elapsed}ms of ${total}ms (${percentage}%)`); | |
} | |
this.start = null; | |
this.count = 0; | |
this.elapsed = 0; | |
}); | |
} | |
get length() { | |
return this.inner.length; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment