Skip to content

How to code for optimizations/minimizations #2

Description

@iklotzko

Often people will use your library with other software, goog closures, etc..., where functions change name and can't be referred to by a literal string with square bracket notation.

Example:

function aNiceProperName() {};
aNiceProperName(); // function is called without problem
window['aNiceProperName'].apply(null, []); // this will result in error (there's no function with this name because during compilation aNiceProperName may translate to M

Here is how I have obviated that for a very small portion of the code for only 2 intersect shape combinations. There's no error checking, it's minimal, but it applies the same ideas in the original code but it is "compilation friendly now".

var IntersectionMethodMap = {
        'intersectEllipseRectangle': function(context, params) {return Intersection.intersectEllipseRectangle.apply(context, params);},
        'intersectLineRectangle': function(context, params) {return Intersection.intersectLineRectangle.apply(context, params);}
    };
    Intersection.intersectShapes = function (shape1, shape2) {
        var ip1 = shape1.getIntersectionParams();
        var ip2 = shape2.getIntersectionParams();
        var result;
        if (ip1 != null && ip2 != null) {
            if (ip1.name == "Path") {
                result = Intersection.intersectPathShape(shape1, shape2);
            } else if (ip2.name == "Path") {
                result = Intersection.intersectPathShape(shape2, shape1);
            } else {
                var method;
                var params;
                if (ip1.name < ip2.name) {
                    method = "intersect" + ip1.name + ip2.name;
                    params = ip1.params.concat(ip2.params);
                } else {
                    method = "intersect" + ip2.name + ip1.name;
                    params = ip2.params.concat(ip1.params);
                }
                result = IntersectionMethodMap[method](null, params);
            }
        } else {
            result = new Intersection("No Intersection");
        }
        return result;
    };
'''

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions