Skip to content

Instantly share code, notes, and snippets.

View pvdz's full-sized avatar

Peter van der Zee pvdz

View GitHub Profile
@pvdz
pvdz / gist:3891556
Created October 15, 2012 08:56
Rewriting expressions, down the rabbit hole you go!
expr +150 -150
100 => 250 -50
-100 => 50 -250
x => x+150 x-150
x+100 => x+250 x-50
x-100 => x+50 x-250
x*100 => x*100+150 x*100-150
x*-100 => x*100+50 x*100-250
x++ => x++ +150 x++ -150
@pvdz
pvdz / gist:4528967
Created January 14, 2013 09:50
Minimal TypeScript compile case. Ouch..
function compile(source) { // derived from www.typescriptlang.org/Playground/
var result = '';
var outfile = {Write:function(s){ result += s; }, WriteLine:function(s){ result += s + '\n'; }, Close:function(){}};
var compiler = new TypeScript.TypeScriptCompiler();
compiler.setErrorCallback(function(start, len, message) { console.log('error',arguments); });
compiler.parser.errorRecovery = true;
compiler.addUnit(source, "input.ts");
compiler.emit(function createFile(fileName) { return outfile; });
return result;
@pvdz
pvdz / gist:4756132
Last active December 12, 2015 09:59
- removed whitespace - made a start with var name normalization - refactored $m into a single return statement (added $_ to help, but since the fingerprint falls away in expression, the overhead is minor) - refactored $n into a single "return" statement. the return value is not used, but it allows for making it look the same as other funcs - ref…

Unconed's failrail : http://jsbin.com/edepor/1/edit

This is a minification attempt by me. See gist history for a few in-between commits. End result is 1452 bytes by jscrush (http://www.iteral.com/jscrush/), that's where I leave it.

I've not even tested this code, I don't care myself if it doesn't even run anymore (but would be pretty sweet if it did! ;)).

The dollar signs allow you to find usages of a variable. The dollar sign itself is not used in the program, so you can remove all of them for "production" minification. As long as they're there, you can figure out where a variable is used (use case sensitive search) very easy this way.

@pvdz
pvdz / gist:5128700
Created March 10, 2013 14:07
requirejs fooky wat Why won't it just work as the example says?
// using requirejs 2.1.5
// see second example after http://requirejs.org/docs/plugins.html#apiload
define({
load: function (name, req, onload, config) {
if (name.slice(0,7) === 'js/user') {
var parse = function(err, source){
if (err) return console.error(err), onload.error(err); // error ajax?
// process source
var par = toTree(source);
@pvdz
pvdz / gist:5433721
Created April 22, 2013 10:08
jsgif foo
var encoder = new GIFEncoder();
encoder.setRepeat(0); // number of times the movie should loop, 0 = forever
encoder.setDelay(Math.floor(1000/settings.fps)); // ms delay between frames
encoder.start();
encoder.addFrame(canvas.getContext('2d'));
encoder.finish();
var binary_gif = encoder.stream().getData();
var data_url = 'data:image/gif;base64,'+Base64.encode(binary_gif);
@pvdz
pvdz / gist:5531788
Created May 7, 2013 10:50
Welcome to my (new) world
for (a in b);
for (location in b);
for (a in location);
for (location in parent);
for (var a in b);
for (var location in b);
for (var a in location);
for (var location in parent);
@pvdz
pvdz / x.js
Last active December 17, 2015 18:19
Original example Rewrite to dynamic var: https://gist.github.com/qfox/5652201 Rewrite to global var: https://gist.github.com/qfox/5652207
// Rewrite to dynamic var: https://gist.github.com/qfox/5652201
// Rewrite to global var: https://gist.github.com/qfox/5652207
// original
function start(x){
var foo = 0;
if (x === 1) foo = 1;
else if (x === 2) foo = 2;
else if (x === 3) foo = 3;
// original: https://gist.github.com/qfox/5652198
// this would use dynamic variable access to store vars from different funcs
// dynamic prop access could very well kill optimizations though
// step1: change var
var $state = {};
function start(x){
$state['start:foo'] = 0;
@pvdz
pvdz / x.js
Last active December 17, 2015 18:19
// original: https://gist.github.com/qfox/5652198
// this rewrite tries to use globals for any local vars
// you'll have the burden of extra scopes, but it beats dynamic vars (https://gist.github.com/qfox/5652201)
// step1: change var
var $start_foo;
function start(x){
$start_foo = 0;
if (x === 1) $start_foo = 1;
var call = Function.prototype.call;
var apply = Function.prototype.apply;
call.call = call;
call.apply = apply;
apply.call = call;
apply.apply = apply;
var hop = Object.prototype.hasOwnProperty;
var hasOwnProperty = function(obj, prop){
return call.call(hop, obj, prop);
};