Last active
May 9, 2017 05:49
-
-
Save slugbyte/4f6229b0e5a19f76ac547bfca8e11bdf to your computer and use it in GitHub Desktop.
a javascript object thats props are dynamicly live reddit json
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
// paste this in the console on jquery.com to tests | |
redditFetch = (title) => $.getJSON(`https://www.reddit.com/r/${title}.json`) | |
reddit = new Proxy({}, { | |
get: (obj, prop) => redditFetch(prop).then(res => res.data) | |
}) | |
// reddit.(anyredditpage).then(callback) | |
reddit.programming.then(console.log) | |
reddit.javascript.then(console.log) | |
// also a self cacheing vesrion | |
lazyReddit = new Proxy({}, { | |
get: (obj , prop) => { | |
if(obj[prop]) return Promise.resolve(obj[prop]) | |
return redditFetch(prop).then(res => { | |
obj[prop] = res.data | |
return res.data; | |
}) | |
} | |
}) | |
console.time('slow') | |
lazyReddit.programming | |
.then(data => { | |
console.log(data) | |
console.timeEnd('slow') // 300ms | |
// but now that its cached | |
console.time('fast') | |
return lazyReddit.programming | |
}) | |
.then(data => { | |
console.log(data) | |
console.timeEnd('fast') // 0.7ms | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment