Created
March 5, 2021 17:16
-
-
Save deepakshrma/b0b480ad8951306534dd47aa767e4dbb to your computer and use it in GitHub Desktop.
How you should not write code-2
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
// require_files/user.json | |
[ | |
{ | |
"_id": "5dea8219b7e4e8016a6661e0", | |
"index": 0, | |
"address": "231 Vandervoort Place, Hollins, South Carolina, 2782", | |
"about": "Ex nulla do duis exercitation Lorem occaecat veniam tempor voluptate laboris adipisicing proident incididunt incididunt. Aliqua enim duis quis esse eiusmod eiusmod tempor velit duis qui anim in. Ipsum incididunt occaecat eu sint. Ut nulla est commodo laborum minim incididunt proident nostrud consectetur tempor. Eiusmod aute tempor eu aute enim nulla ut aliquip. Pariatur consequat tempor tempor irure sunt cillum. Ullamco Lorem Lorem ullamco enim ex elit ut pariatur in qui.\r\n", | |
"greeting": "Hello, Cherie Webster! You have 7 unread messages.", | |
"favoriteFruit": "strawberry" | |
} | |
] |
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
// req.js | |
function main() { | |
console.time("REQUIRE"); | |
const user = require("./user.json"); | |
console.timeEnd("REQUIRE"); | |
console.time("REQUIRE-2"); | |
const userJson = JSON.parse(fs.readFileSync(__dirname + "/user.json")); | |
console.timeEnd("REQUIRE-2"); | |
} | |
main(); | |
// output | |
// REQUIRE: 0.989ms | |
// REQUIRE-2: 0.245ms |
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
function main() { | |
console.time("REQUIRE"); | |
const user = require("./user.json"); | |
console.timeEnd("REQUIRE"); | |
console.time("REQUIRE-2"); | |
const userJson = JSON.parse(fs.readFileSync(__dirname + "/user.json")); | |
console.timeEnd("REQUIRE-2"); | |
console.time("REQUIRE-FOR"); | |
for (let e = 0; e < 10000; e++) | |
require("./user.json"); | |
console.timeEnd("REQUIRE-FOR"); | |
console.time("REQUIRE-FOR-2"); | |
for (let e = 0; e < 10000; e++) | |
JSON.parse(fs.readFileSync(__dirname + "/user.json")); | |
console.timeEnd("REQUIRE-FOR-2"); | |
} | |
main(); | |
// Output | |
// REQUIRE: 0.956ms | |
// REQUIRE-2: 0.190ms | |
// REQUIRE-FOR: 164.483ms | |
// REQUIRE-FOR-2: 663.703ms |
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
// util.js | |
const fs = require("fs"); | |
let files = {}; | |
function requireJSON(path) { | |
if (!files[path]) { | |
files[path] = JSON.parse(fs.readFileSync(path)); | |
} | |
return files[path]; | |
} | |
module.exports = { | |
requireJSON | |
}; | |
// req.js | |
const fs = require("fs"); | |
const { requireJSON } = require("./util"); | |
function main() { | |
// ...Rest of the code | |
console.time("REQUIRE-FOR-NEW"); | |
for (let e = 0; e < 10000; e++) { | |
require("./user.json"); | |
} | |
console.timeEnd("REQUIRE-FOR-NEW"); | |
console.time("REQUIRE-FOR-NEW-2"); | |
for (let e = 0; e < 10000; e++) { | |
requireJSON(__dirname + "/user.json"); | |
} | |
console.timeEnd("REQUIRE-FOR-NEW-2"); | |
} | |
main(); | |
// Output | |
// REQUIRE: 0.737ms | |
// REQUIRE-2: 0.196ms | |
// REQUIRE-FOR: 170.930ms | |
// REQUIRE-FOR-2: 608.423ms | |
// REQUIRE-FOR-NEW: 147.607ms | |
// REQUIRE-FOR-NEW-2: 3.071ms |
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
//util.js | |
function requireFile(path) { | |
if (!files[path]) { | |
files[path] = fs.readFileSync(path); | |
} | |
return files[path]; | |
} | |
// sync_server.js | |
const http = require('http'); | |
const {requireFile} = require("../require_files/util") | |
const server = http.createServer(); | |
server.on('request', async (req, res) => { | |
res.end(requireFile(__dirname+"/users.json")); | |
}); | |
server.listen(8080); | |
// no_sync_server.js | |
const http = require('http'); | |
const fs = require('fs'); | |
const server = http.createServer(); | |
server.on('request', async (req, res) => { | |
fs.createReadStream(__dirname+"/users.json").pipe(res) | |
}); | |
server.listen(8080); |
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
// utils.js | |
function requireFileNoCache(path) { | |
return fs.readFileSync(path); | |
} | |
// js/no_sync/sync_server_no_cache.js | |
const http = require('http'); | |
const {requireFileNoCache} = require("../require_files/util") | |
const server = http.createServer(); | |
server.on('request', async (req, res) => { | |
res.end(requireFileNoCache(__dirname+"/users.json")); | |
}); | |
server.listen(8080); |
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
function firstLetters(words) { | |
return _.map(words, (word) => { | |
return _.first(word) | |
}) | |
} | |
console.log(firstLetters(["Test", "Rest"])) | |
// [ "T", "R"] |
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
const first = function(str) { | |
return str[0]; | |
}; | |
const map = function(fun, arr) { | |
return arr.map(fun); | |
}; | |
console.log(first("Test")); | |
console.log(map(first, ["Test", "Rest"])); | |
// ["T", "R"] |
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
const curry = (fn, arity = fn.length, ...args) => | |
arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args); | |
const first = function(str) { | |
return str[0]; | |
}; | |
const map = curry(function(fun, arr) { | |
return arr.map(fun); | |
}); | |
const firstLetters = map(first); | |
console.log(firstLetters(["Test", "Rest"])); | |
// ["T", "R"] |
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
const obj = { | |
"IN": "India", | |
"SG": "Singapore", | |
"US": "United State Of America", | |
"JP": "JAPAN", | |
"UK": "United Kingdom" | |
} | |
// get all keys | |
console.log(Object.keys(obj)) | |
// get all key-value pair entries | |
console.log(Object.entries(obj)) | |
// get all countries array | |
console.time("entries") | |
console.log(Object.entries(obj).map(([code, name]) => { | |
return { | |
name, | |
code | |
} | |
})) | |
console.timeEnd("entries") | |
// Output | |
[ { name: 'India', code: 'IN' }, | |
{ name: 'Singapore', code: 'SG' }, | |
{ name: 'United State Of America', code: 'US' }, | |
{ name: 'JAPAN', code: 'JP' }, | |
{ name: 'United Kingdom', code: 'UK' } ] | |
entries: 0.740ms |
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
// flatObject in array | |
console.time("flatObject") | |
const flatObject = (obj) => { | |
let array = [] | |
for(let key in obj) { | |
array.push({name: obj[key], code: key}) | |
} | |
return array | |
} | |
console.log(flatObject(obj)) | |
console.timeEnd("flatObject") | |
//Output: | |
[ { name: 'India', code: 'IN' }, | |
{ name: 'Singapore', code: 'SG' }, | |
{ name: 'United State Of America', code: 'US' }, | |
{ name: 'JAPAN', code: 'JP' }, | |
{ name: 'United Kingdom', code: 'UK' } ] | |
flatObject: 0.415ms |
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
// flatObject in array, with mapper function | |
console.time("flatObjectFn") | |
// fun object, function -> object[] | |
const flatObjectFn = (obj, fn) => { | |
let array = [] | |
for(let key in obj) { | |
array.push(fn(key, obj[key])) | |
} | |
return array | |
} | |
console.log(flatObjectFn(obj, (key, value) => ({name:value, code: key}))) | |
console.timeEnd("flatObjectFn") | |
//Output | |
[ { name: 'India', code: 'IN' }, | |
{ name: 'Singapore', code: 'SG' }, | |
{ name: 'United State Of America', code: 'US' }, | |
{ name: 'JAPAN', code: 'JP' }, | |
{ name: 'United Kingdom', code: 'UK' } ] | |
flatObjectFn: 0.128ms |
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
const delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args); | |
const resolve = () => { | |
return new Promise(r => { | |
delay(r, 1000, "SUCCESS"); | |
}); | |
}; | |
let all = Promise.all([resolve(), resolve()]); | |
all.then(console.log).catch(console.error); | |
// [ 'SUCCESS', 'SUCCESS' ] |
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
const delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args); | |
const resolve = () => { | |
return new Promise(r => { | |
delay(r, 1000, "SUCCESS"); | |
}); | |
}; | |
const reject = () => { | |
return new Promise((_, r) => { | |
delay(r, 800, "FAIL"); | |
}); | |
}; | |
let all = Promise.all([resolve(), reject()]); | |
all.then(console.log).catch(console.error); | |
// Outputs | |
FAIL |
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
// Type definition | |
/** | |
* Creates a Promise that is resolved with an array of results when all of the provided Promises | |
* resolve, or rejected when any Promise is rejected. | |
* @param values An array of Promises. | |
* @returns A new Promise. | |
*/ | |
//all<T>(values: (T | PromiseLike<T>)[]): Promise<T[]>; |
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
const collectAll = (...promises) => { | |
return new Promise(r => { | |
const results = []; | |
let counter = 0; | |
const final = (index, res) => { | |
results[index] = res; | |
counter++; | |
if (counter === promises.length) r(results); | |
}; | |
promises.map((promise, index) => { | |
promise | |
.then(final.bind(null, index)) | |
.catch(final.bind(null, index)); | |
}); | |
}); | |
}; | |
let all = collectAll(resolve(), resolve()); | |
all.then(console.log) | |
all = collectAll(resolve(), reject()); | |
all.then(console.log) | |
// Output | |
[ 'SUCCESS', 'SUCCESS' ] | |
[ 'SUCCESS', 'FAIL' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment