Skip to content

Instantly share code, notes, and snippets.

@nemtsov
Last active January 31, 2019 04:46
Show Gist options
  • Save nemtsov/75806b9d32e908393c0f2fcab38fcfa7 to your computer and use it in GitHub Desktop.
Save nemtsov/75806b9d32e908393c0f2fcab38fcfa7 to your computer and use it in GitHub Desktop.
Memory Leak Talk
const http = require('http');
const colors = ['red', 'green', 'blue'];
function getUserType(user) {
// - long or cpu-intensive task -
switch (user.color) {
case 'red':
return 'red user';
case 'green':
return 'green user';
case 'blue':
return 'blue user';
}
}
const server = http.createServer((req, res) => {
const user = {
color: colors[Math.floor(Math.random() * 3)]
};
res.end(getUserType(user));
});
server.listen(3000);
const http = require('http');
const colors = ['red', 'green', 'blue'];
const cache = {};
function getUserType(user) {
// - long or cpu-intensive task -
switch (user.color) {
case 'red':
return 'red user';
case 'green':
return 'green user';
case 'blue':
return 'blue user';
}
}
const server = http.createServer((req, res) => {
const user = {
color: colors[Math.floor(Math.random() * 3)]
};
const cacheKey = JSON.stringify(user);
if (!cache[cacheKey]) {
cache[cacheKey] = getUserType(user);
}
res.end(cache[cacheKey]);
});
server.listen(3000);
const http = require('http');
const crypto = require('crypto');
const colors = ['red', 'green', 'blue'];
const cache = {};
function getUserType(user) {
// - long or cpu-intensive task -
switch (user.color) {
case 'red':
return 'red user';
case 'green':
return 'green user';
case 'blue':
return 'blue user';
}
}
const server = http.createServer((req, res) => {
const user = {
uid: crypto.randomBytes(10).toString('hex'),
color: colors[Math.floor(Math.random() * 3)]
};
const cacheKey = JSON.stringify(user);
if (!cache[cacheKey]) {
cache[cacheKey] = getUserType(user);
}
res.end(cache[cacheKey]);
});
server.listen(3000);
const a = {};
setInterval(() => {
a[Math.random()] = true;
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment