Created
August 11, 2011 23:34
-
-
Save masonicboom/1141076 to your computer and use it in GitHub Desktop.
A benchmark to compare inserts into HTML5 localStorage vs. HTML5 WebSQL.
This file contains hidden or 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
| /* A benchmark to compare inserts into HTML5 localStorage vs. | |
| * HTML5 WebSQL. Only tested on Google Chrome. | |
| * | |
| * Written by Mason Simon (https://plus.google.com/113533580716771345509). | |
| */ | |
| /* Returns a function to for timing incremental progress towards | |
| * a goal, N. When N is reached, calls finishedHandler with the | |
| * total time to reach N, in milliseconds. | |
| */ | |
| function buildTallyFunc(N, finishedHandler) { | |
| var numFinished = 0; | |
| var startMillis = Date.now(); | |
| var tallyFunc = function() { | |
| numFinished += 1; | |
| if (numFinished >= N) { | |
| var elapsedMillis = Date.now() - startMillis; | |
| finishedHandler(elapsedMillis); | |
| } | |
| } | |
| return tallyFunc; | |
| } | |
| function benchmarkLocalStorage(N, finishedHandler) { | |
| // Initialize environment. | |
| localStorage.clear(); | |
| // Run benchmark. | |
| var tally = buildTallyFunc(N, finishedHandler); | |
| for (var i = 0; i < N; i++) { | |
| localStorage[i] = 'x'; | |
| tally(); | |
| } | |
| } | |
| function benchmarkWebSql(N, finishedHandler) { | |
| // Initialize environment. | |
| var db = openDatabase('benchmark', '1.0', '', 1 * 1024 * 1024); | |
| db.transaction(function(tx) { | |
| tx.executeSql('DROP TABLE benchmark;'); | |
| tx.executeSql('CREATE TABLE benchmark (id unique, value);'); | |
| }); | |
| // Run benchmark. | |
| var tally = buildTallyFunc(N, finishedHandler); | |
| for (var i = 0; i < N; i++) { | |
| db.transaction(function(tx) { | |
| tx.executeSql('INSERT INTO benchmark (id, value) VALUES (?, ?);', [i, 'x']); | |
| tally(); | |
| }); | |
| } | |
| } | |
| function recordResults(N, localStorageMillis, webSqlMillis) { | |
| document.writeln('Time to insert ' + N + ' records:<br/><b>localStorage</b> ' + localStorageMillis + 'ms<br/><b>WebSQL</b> ' + webSqlMillis + 'ms'); | |
| } | |
| function runBenchmarks(N) { | |
| // Randomly pick which benchmark to run first. | |
| if (Math.random() > 0.5) { | |
| // Benchmark localStorage first. | |
| benchmarkLocalStorage(N, function(localStorageElapsedMillis) { | |
| benchmarkWebSql(N, function(webSqlElapsedMillis) { | |
| recordResults(N, localStorageElapsedMillis, webSqlElapsedMillis); | |
| }); | |
| }); | |
| } else { | |
| // Benchmark WebSQL first. | |
| benchmarkWebSql(N, function(webSqlElapsedMillis) { | |
| benchmarkLocalStorage(N, function(localStorageElapsedMillis) { | |
| recordResults(N, localStorageElapsedMillis, webSqlElapsedMillis); | |
| }); | |
| }); | |
| } | |
| } | |
| runBenchmarks(1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment