Skip to content

Performance Slowness with Flatten function #525

@baholladay

Description

@baholladay

I found the the Flatten function is extremely slow when dealing with 40k+ text nodes. The issue is that it is using the javascript "concat" function, which is extremely slow. This function works 1000x faster and returns the same result:

return jq.map(function() {
            function isArray(obj) {
                return toString.call(obj) === '[object Array]';
            };

            function flatten(branch, flat, max, depth) {
                flat = flat || [ ]; max = max || 1;
                depth = ( depth ? depth + 1 : 1);
                for(var i in branch) {
                    if(isArray(branch[i])) {
                        if((max < 0) || (depth <= max)) {
                            flatten(branch[i], flat, max, depth);
                        } else { flat.push(branch[i]); }
                    } else { flat.push(branch[i]); }
                }
                return flat;
            };          
            return flatten(getTextNodes(this), [], -1, -1);
        })

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions