extract-values icon indicating copy to clipboard operation
extract-values copied to clipboard

Compile before parsing for performance

Open rijnhard opened this issue 6 years ago • 1 comments

Hi

So I messed around and rewrote this to allow for compiling ahead before parsing and got some significant performance increases. I tested using your test case data.

https://jsperf.com/string-value-extraction/1

code

const RGX_SPECIAL_CHARS = /[\\\^\$\*\+\.\?\(\)]/g, // eslint-disable-line
    RGX_WHITESPACE = /\s+/g;

/**
 * compile ahead version of extract values
 * @see https://github.com/laktek/extract-values
 */
class ExtractValues {
    /**
     * @member {Object} ExtractValues~config
     * @private
     */

    /**
     * @member {string} ExtractValues~template
     * @private
     */

    /**
     * @member {string[]|null} ExtractValues~tokens
     * @private
     */

    /**
     * @member {RegExp} ExtractValues~extractor
     * @private
     */

    /**
     *
     * @param template
     * @param options
     */
    constructor(template, options = {}) {
        options = Object.assign({
            delimiters: ['{', '}'],
            lowercase : false,
            whitespace: null
        }, options);

        // validate

        const tokenizer = new RegExp(`${options.delimiters[0]}([^${options.delimiters.join('')}\t\r\n]+)${options.delimiters[1]}`, 'g'),
            tokenNormalizer = new RegExp(`${options.delimiters[0]}|${options.delimiters[1]}`, 'g');

        let tokens = template.match(tokenizer);

        if (tokens) {
            tokens = tokens.map(token => token.replace(tokenNormalizer, '').trim());
        }

        Object.defineProperties(this, {
            config: {
                enumerable: true,
                value     : options
            },
            template: {
                enumerable: true,
                value     : template
            },
            tokens: {
                enumerable: true,
                value     : tokens
            },
            extractor: {
                enumerable: true,
                value     : new RegExp(template.replace(RGX_SPECIAL_CHARS, '\\$&').replace(tokenizer, '(\.+)'))
            }
        });
    }

    /**
     * parses the input according to the compiles template
     * @param {string} str
     * @return {Object<string,string>|null}
     */
    parse(str) {
        if (this.config.lowercase) {
            str = str.toLowerCase();
        }

        if (this.config.whitespace != null) {
            const whitespaced = ' '.repeat(this.config.whitespace);

            str = str.replace(RGX_WHITESPACE, whitespaced);
        }

        let matches = str.match(this.extractor);

        if (!matches) {
            return null;
        }

        // Allow exact string matches to return an empty object instead of null
        if (!this.tokens) {
            return (str === this.template) ? {} : null;
        }

        matches = matches.splice(1);
        const output = {};

        for (let i = 0; i < this.tokens.length; i += 1) {
            output[this.tokens[i]] = matches[i];
        }

        return output;
    }
}

The only downside was that it was 30% or so slower if you had to create a new instance for every parse. I'm pretty sure that that is due to object creation and there's probably faster ways to do that, but for my cases I'll stick with the class since I will almost always compile ahead.

rijnhard avatar Jun 07 '18 14:06 rijnhard

looks like I don't fully understand how your whitespace handling works, so that needs to be fixed

rijnhard avatar Jun 07 '18 15:06 rijnhard