Skip to content

Instantly share code, notes, and snippets.

View alessioalex's full-sized avatar

Alexandru Vlăduţu alessioalex

View GitHub Profile
@alessioalex
alessioalex / 0-cache-second-try.js
Created November 22, 2014 08:43
0-cache-second-try.js
// our own in memory database
var users = {
'alex': { user: 'alex', age: 27 },
'john': { user: 'johndoe', age: 34 }
};
// async function
var getUserFromDb = function getUserFromDb(id, callback) {
var min = 1000;
var max = 2000;
@alessioalex
alessioalex / 0-cache-args.js
Created November 22, 2014 08:49
0-cache-args.js
// our own in memory database
var users = {
'alex': { user: 'alex', age: 27 },
'john': { user: 'johndoe', age: 34 }
};
// async function
var getUserFromDb = function getUserFromDb(id, callback) {
var min = 1000;
var max = 2000;
@alessioalex
alessioalex / heavy-computation-settimeout.js
Created November 22, 2014 09:08
heavy-computation-settimeout.js
// dividing it into chunks
var getSum = function getSumOfEvenNumbers(opts, cb) {
var sum = opts.sum || 0;
var i = opts.i || 0;
var stopAt = opts.stopAt;
var CHUNK_SIZE = 1000000;
var nextStop = (stopAt < (i + CHUNK_SIZE)) ? stopAt : (i + CHUNK_SIZE);
for (; i < nextStop; i++) {
@alessioalex
alessioalex / settimeout-example.js
Created November 22, 2014 09:12
settimeout-example.js
var log = function(i) {
console.log(i);
}
function tellMeLater() {
for (var i = 0; i < 10; i++) {
setTimeout(log.bind(null, i), 100);
}
}
@alessioalex
alessioalex / settimeout-iif.js
Created November 22, 2014 09:15
settimeout-iif.js
function tellMeLater() {
for (var i = 0; i < 10; i++) {
(function(i) {
setTimeout(function() {
console.log(i);
}, 100);
}(i));
}
}
@alessioalex
alessioalex / inheritance-0.js
Created November 22, 2014 09:17
inheritance-0.js
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.getInfoCard = function() {
return this.name + ', ' + this.age + ' years old';
};
var johnny = new Person('John', '25');
@alessioalex
alessioalex / inheritance-2.js
Created November 22, 2014 09:32
inheritance-2.js
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.getInfoCard = function() {
return this.name + ', ' + this.age + ' years old';
};
// function Student(name, age, university) {
@alessioalex
alessioalex / custom-writable-stream.js
Created November 22, 2014 12:47
custom-writable-stream.js
// example from http://codewinds.com/blog/2013-08-19-nodejs-writable-streams.html
"use strict";
var stream = require('stream');
var util = require('util');
// use Node.js Writable, otherwise load polyfill
var Writable = stream.Writable;
var memStore = { };
@alessioalex
alessioalex / custom-transform-stream.js
Created November 22, 2014 12:54
custom-transform-stream.js
// http://codewinds.com/blog/2013-08-20-nodejs-transform-streams.html
"use strict";
var crypto = require('crypto');
var stream = require('stream');
var util = require('util');
var Transform = stream.Transform;
function ShaSum(options) {
@alessioalex
alessioalex / calling-without-new.js
Created November 22, 2014 13:04
calling-without-new.js
function Student(name, age, university) {
if (!(this instanceof Student)) {
return new Student(name, age, university);
}
// #1 - duplicate code from Person constructor
//Person.call(this, name, age);
this.university = university;
}
var junior2 = Student('John', 18, 'MIT');