Skip to content

Instantly share code, notes, and snippets.

@lewisje
Last active August 29, 2015 14:25

Revisions

  1. lewisje revised this gist Jul 22, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion OptimizationRevivers.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    function functionize(func, arg) {
    switch (typeof func) {
    case 'string':
    return new Function(func, String(arg));
    return arg ? new Function(String(arg), func) : new Function(func);
    case 'function':
    return func;
    default:
  2. lewisje created this gist Jul 22, 2015.
    30 changes: 30 additions & 0 deletions OptimizationRevivers.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    // part of a pair of functions intended to isolate code that kills the optimizing compiler
    // https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#2-unsupported-syntax
    function functionize(func, arg) {
    switch (typeof func) {
    case 'string':
    return new Function(func, String(arg));
    case 'function':
    return func;
    default:
    return function () {return func;};
    }
    }
    // The first argument to the toCatch callback is the caught error;
    // if toCatch is passed as a string, this argument must be named `e`.
    function trial(toTry, toCatch, toFinal) {
    var try1 = functionize(toTry),
    catch1 = functionize(toCatch, 'e'),
    final1 = functionize(toFinal);
    try {try1();}
    catch (e) {catch1(e);}
    finally {final1();}
    }
    // This does not directly replace try/catch/finally blocks that
    // would break or continue from loops or return from the function:
    // Consider setting a variable indicating success or failure within
    // each callback and then deciding to break, continue, or return.

    // This is a reliable way to get a reference to the global object
    // even in strict mode, without deoptimizing the entire function.
    var global = function(){return this||(1,eval)('this');}();