Skip to content

Instantly share code, notes, and snippets.

@uu59
Created March 6, 2012 11:17
Show Gist options
  • Save uu59/1985732 to your computer and use it in GitHub Desktop.
Save uu59/1985732 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="ja">
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var LS = {
_: {},
_timer: null,
setItem: function(k,v){
var _ = this._;
_[k] = v;
setTimeout(function(){
localStorage.setItem(k, _[k]);
}, 64);
},
getItem: function(k,v){
return this._[k] || localStorage.getItem(k);
}
}
var TIMES = 10000;
function bench(label, fn){
var start = new Date;
for(var i=0; i<TIMES; i++) {
fn();
}
var end = new Date;
var ms = end.getTime() - start.getTime();
console.log(label, ms);
}
bench("localStorage", function(){
localStorage.setItem('foo', "foo");
});
bench("cookie", function(){
document.cookie.foo = "foo";
});
var tmp = {};
bench("object", function(){
tmp["foo"] = "foo";
});
bench("LS", function(){
LS.setItem('foo', "foo");
});
bench("localStorage(get)", function(){
return localStorage.getItem('foo');
});
bench("cookie(get)", function(){
return document.cookie.foo;
});
bench("object(get)", function(){
return tmp["foo"];
});
bench("LS(get)", function(){
return LS.getItem('foo');
});
</script>
</body></html>
@uu59
Copy link
Author

uu59 commented Mar 6, 2012

Intel X25-E SSD
Chromium 18.0.997.0 (Developer Build 116462 Linux) Built on Ubuntu 11.04, running on Ubuntu 11.10

on my computer result:

localStorage 2890
cookie 1026
object 0
LS 27
localStorage(get) 2343
cookie(get) 1380
object(get) 0
LS(get) 1

@uu59
Copy link
Author

uu59 commented Mar 6, 2012

This is just for demonstration that this is why lack of any mechanism/feature.
You shouldn't use this in production env.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment