Last active
December 22, 2015 16:49
-
-
Save motss/d3f89cfc56a8fbf414c2 to your computer and use it in GitHub Desktop.
Comparison between getting data from Firebase using sync and async
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
// PART 1a - Uncomment the following lines if running on Node; | |
// var _ = require("lodash"); | |
// var Firebase = require('firebase'); | |
// PART 1b - Uncomment the following lines if running on Chrome Dev Tool; | |
// var script = document.createElement('script'); | |
// script.type = 'text/javascript'; | |
// script.src = 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js'; | |
// document.head.appendChild(script); | |
// var script = document.createElement('script'); | |
// script.type = 'text/javascript'; | |
// script.src = 'https://cdn.firebase.com/js/client/2.3.2/firebase.js'; | |
// document.head.appendChild(script); | |
// PART2 - Rewrite firebaseRef to point to the location you wish to get data from; | |
var firebaseRef = 'https://<your_firebase_ref>.firebaseio.com'; | |
// To be able to log elapsed time on both Node and browser; | |
var logElapseTime = function (_endLabel) { | |
try { | |
if (_endLabel) { | |
return process.hrtime(_endLabel); | |
} | |
return process.hrtime(); | |
}catch (e) { | |
return window.performance.now(); | |
} | |
}; | |
var haha = logElapseTime(); | |
// Sync Approach; | |
console.log('Running sync task...\n'); | |
firebaseRef.once('value', function (snapshot) { | |
if (snapshot) { | |
var hahaEnd = logElapseTime(haha); | |
console.log('\n1) Time spent on sync: %dms', _.isArray(hahaEnd) ? (hahaEnd[0] * 1E3 + hahaEnd[1] * 1E-6).toFixed(3) : (hahaEnd - haha)); | |
} | |
}); | |
var haha2 = logElapseTime(); | |
firebaseRef.once('value', function (snapshot) { | |
if (snapshot) { | |
var hahaEnd2 = logElapseTime(haha2); | |
console.log('\n2) Time spent on sync 2: %dms', _.isArray(hahaEnd2) ? (hahaEnd2[0] * 1E3 + hahaEnd2[1] * 1E-6).toFixed(3) : (hahaEnd2 - haha2)); | |
} | |
}); | |
console.log('Separator...\n'); | |
// Async approach using ES2015's Promise; | |
var lol = logElapseTime(); | |
console.log('Running async task...\n'); | |
var _p1 = new Promise(function (resolve, reject) { | |
firebaseRef.once('value', function (snapshot) { | |
resolve(snapshot); | |
}); | |
}); | |
var _p2 = new Promise(function (resolve, reject) { | |
firebaseRef.once('value', function (snapshot) { | |
resolve(snapshot); | |
}); | |
}); | |
Promise.all([_p1, _p2]).then(function (value) { | |
if (value) { | |
var lolEnd = logElapseTime(lol); | |
console.log('\n3) Time spent on async: %dms', _.isArray(lolEnd) ? (lolEnd[0] * 1E3 + lolEnd[1] * 1E-6).toFixed(3) : (lolEnd - lol)); | |
} | |
}); | |
// Running successive async tasks using ES2015's Promise; | |
// TAT reduces after all; | |
var successiveIdx = 4; | |
// stop running successive async tasks after 60s; | |
var stopSuccessiveTimer = 60; | |
var successiveTimer = setInterval(function () { | |
console.log('\n\n#############################'); | |
console.log('Successive Separator...\n'); | |
var successive = logElapseTime(); | |
console.log('Running successive async tasks...'); | |
var _p3 = new Promise(function (resolve, reject) { | |
firebaseRef.once('value', function (snapshot) { | |
resolve(snapshot); | |
}); | |
}); | |
var _p4 = new Promise(function (resolve, reject) { | |
firebaseRef.once('value', function (snapshot) { | |
resolve(snapshot); | |
}); | |
}); | |
Promise.all([_p4, _p3]).then(function (value) { | |
if (value) { | |
var successiveEnd = logElapseTime(successive); | |
console.log('\n%d) Time spent on successive async: %dms', successiveIdx, _.isArray(successiveEnd) ? (successiveEnd[0] * 1E3 + successiveEnd[1] * 1E-6).toFixed(3) : (successiveEnd - successive)); | |
} | |
}); | |
successiveIdx++; | |
}, 1000); | |
setTimeout(function () { | |
clearTimeout(successiveTimer); | |
}, (stopSuccessiveTime * 1000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment