AlexeyGrishin / schemasaurus

JSON schema iterator & validator
ISC License
11 stars 2 forks source link

Eliminate eval'ish Function constructor usage #1

Open brettz9 opened 9 years ago

brettz9 commented 9 years ago

Would you be open to a PR to get rid of the Function usage? This is discouraged practice and can be implemented by other means. Thanks!

AlexeyGrishin commented 9 years ago

Thank you for your feedback.

Actually Function is used to precompile code which iterates over schema/object to get performance. I saw that a lot of libraries like template engines (handlebars, jade) or JSON schema validators use Function for the same reason. So I do not think it is really evil here.

First version did not use it, but performance was terrible. How would you suggest to achieve the similar performance without code generation?

brettz9 commented 9 years ago

Do you mean that code generation using such as the following example of a closure pattern has too high of performance problems? I'd actually think that eval() would be slower:

function buildForCallback (i, end, inc, cb) {
    return function () {
        for (; i < end; i += inc) {
            cb();
        }
    };
}