Skip to content

Instantly share code, notes, and snippets.

View getify's full-sized avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@getify
getify / memtest-results.txt
Last active August 29, 2015 13:56
profiling memory usage issues with parsing a big JSON database
> node --expose-gc memtest.js
On my 64bit mac, this process uses about 391mb of memory according to "Activity Monitor".
> node --expose-gc memtest2.js
By contrast, if I read in 3.1mm records nearly identical to those being generated, from a .json file,
this process fills up 1.5gb of memory quickly, then crawls to a halt, and takes a REALLY long time to
get all the records finally pulled in (like hours).
@getify
getify / gist:8902610
Last active August 29, 2015 13:56
how i wish JS generators could have worked
// I wish that generators allowed you to call next(..)
// at the first step with a value, and that value was
// the first (named) argument to the generator itself.
//
// see `next(3)` below setting `msg` to `3`.
//
// It would mirror how `return` is overloaded to do the final
// `yield`: by letting an argument be the initial "yield target"
// of an iterator-sent message.
@getify
getify / gist:8903246
Created February 9, 2014 18:05
function declarations in blocks?
"use strict";
foo(); // "b"
var a = true;
if (a) {
function foo() { console.log("a"); }
}
else {
function foo() { console.log("b"); }
@getify
getify / gist:8911224
Last active August 29, 2015 13:56
another idea that'll never happen
// {?( .. ) opens up a conditionally compiled block.
// inside the ( .. ), you do a test, which is evaluated
// to see if the contents of the block should be compiled
// or not. If success, proceeds as a normal { .. } block
// of code. If test fails, entire contents of block
// are ignored and not compiled (no syntax errors)
//
// This would primarily be useful for using ES.next features
// that have breaking syntax. You construct a FT for the feature
// and if the test succeeds, use it, otherwise skip it.
@getify
getify / gist:8921982
Created February 10, 2014 18:58
using asynquence.map() for an async forEach()
function handleItem(val,cb) {
setTimeout( function(){
console.log( val );
cb();
}, 100 );
}
ASQ()
.map(
[1,2,3,4,5]
@getify
getify / gist:9105362
Last active August 29, 2015 13:56
Exploring why I wish ES6's `for-of` had a syntax for sending in values to each iteration's `next(..)` call
// This example shows a simple data structure object that holds
// a sequence of numbers from 0 to 99. It defines its own iterator
// which lets you dump all those values out using a `for-of` loop.
//
// Note: the iterator accepts a value to `next(..)` which represents
// a step value, if you wanted to get only every nth value from
// the sequence.
//
// Unfortunately, ES6's for-of doesn't have a syntax for passing in
// a value to that default per-iteration `next(..)` call that occurs. :(
@getify
getify / gist:9107067
Created February 20, 2014 04:23
exploring generators + parallel-promises for a single "step"
// is this how most people see CLEANLY doing the task of
// firing off multiple "parallel" tasks at a single step
// in a generator, using parallel promises (aka, `all()`)?
//
// or is there another pattern for "parallel" generator steps?
spawn(function*(){
var [x,y] = yield Promise.all(getValue(10),getValue(12));
var z = yield getValue(20);
console.log("Meaning of life: " + (x+y+z)); // Meaning of life: 42
@getify
getify / gist:9110935
Last active August 29, 2015 13:56
promise sync vs async?
// Hey, what gives here? This is weird and unexpected.
// Why is the promise factory called synchronously, when
// all the other steps resolve async? For consistency,
// one would expect them all to be async.
var p = new Promise(function(resolve,reject){
console.log("what");
resolve();
});
@getify
getify / gist:9117527
Last active August 29, 2015 13:56
Trying to illustrate the sync/async inconsistency between promise initialization and promise chaining.
// For consistency sake, would expect:
// 1, 2, 3, 4, 5
// actually gets, confusingly:
// 1, 2, 4, 3, 5
var p = new Promise(function(resolve,reject){
resolve();
});
@getify
getify / gist:9118029
Last active August 29, 2015 13:56
how sync/async inconsistency + setTimeout hack == race condition
// expected:
// 1, 2, 3, 4, 5
// got:
// 1, 2, 3, 5, 4
// ...
// :(
// by using the setTimeout "hack" that @domenic suggests, now we
// create a race-condition between "4" and "5", and
// confusingly, "5" wins