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
// maxOnlyEven(): undefined
// maxOnlyEven(1): undefined
// maxOnlyEven(1,3): undefined
// maxOnlyEven(1,4): 4
// maxOnlyEven(2): 2
// maxOnlyEven(undefined,2): 2
// maxOnlyEven(2,4): 4
function maxOnlyEven(num1,num2) {
var A = num1 % 2 == 0;
@getify
getify / 1.js
Last active October 6, 2016 07:01
// examples...
//
// maxEven(): undefined
// maxEven(1): undefined
// maxEven(1,13): undefined
// maxEven(-Infinity,Infinity): undefined
// maxEven(0): 0
// maxEven(-4,3,-6,0,5,2,1,9): 2
function maxEven(num1,...nums) {

Parens And Performance

Years ago, some smart folks that worked on JS engines realized that not all JS that's loaded into a page/app initially is needed right away. They implemented JIT to optimize this situation.

JIT means Just-In-Time, which means essentially that the engine can defer processing (parsing, compiling) certain parts of a JS program until a later time, for example when the function in question is actually needed. This deferral means the engine is freer to spend the important cycles right now on the code that's going to run right now. This is a really good thing for JS performance.

Some time later, some JS engine devs realized that they needed to get some hints from the code as to which functions would run right away, and which ones wouldn't. In technical speak, these hints are called heuristics.

So they realized that one very common pattern for knowing that a function was going to run right away is if the first character before the function keyword was a (, because that usually m

@getify
getify / 1.js
Created September 16, 2016 20:19
function whateverYouCallIt(obj) {
function F(){};
F.prototype = obj;
return F;
}
var o = {};
class C extends whateverYouCallIt(o) {}
@getify
getify / 1.js
Last active November 4, 2016 14:39
// All of the following are ways that template literals are *NOT* `strictly better` than " or ' delimited strings
// RE: https://ponyfoo.com/articles/template-literals-strictly-better-strings
// valid syntax, but doesn't turn on strict mode
`use strict`;
// invalid syntax
const x = { `hello world`: 42 };
// invalid syntax
@getify
getify / 1.js
Created September 8, 2016 22:37
trying to figure out how closure works over params and body-vars
function foo(x,y = function(){ return x; }) {
console.log( "a:", x );
var x = 1;
console.log( "b:", y() );
}
foo(2);
@getify
getify / 1.js
Created August 25, 2016 21:47
`super` being static rather than dynamic
class P {
foo() { console.log( "P.foo" ); }
}
class C extends P {
foo() {
super();
}
}
@getify
getify / 1.js
Last active August 25, 2016 16:27
exploring point-free and (maybe?) AOP style
function printIf( msg, predicate ) {
if (predicate( msg )) {
console.log( msg );
}
}
function isShortEnough(str) {
return str.length <= 5;
}
@getify
getify / step1.js
Last active February 28, 2022 16:38
transducing in javascript
function add1(v) { return v + 1; }
function isOdd(v) { return v % 2 == 1; }
function sum(total,v) { return total + v; }
var list = [2,5,8,11,14,17,20];
list
.map( add1 )
.filter( isOdd )
.reduce( sum );
function add1(v) {
return v + 1;
}
function mul2(v) {
return v * 2;
}
function div3(v) {
return v / 3;
}