Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up utils.js to reduce bundle size #10392

Closed
dgirardi opened this issue Aug 24, 2023 · 2 comments · Fixed by #10441
Closed

Clean up utils.js to reduce bundle size #10392

dgirardi opened this issue Aug 24, 2023 · 2 comments · Fixed by #10441
Labels

Comments

@dgirardi
Copy link
Collaborator

Type of issue

improvement

Description

utils.js is simultaneously the largest and least maintained file in the codebase. It can be improved in at least two ways:

  • move logic that is not needed by core into libraries, so that it ends up only in bundles with modules that actually need it. As example, this:

    Prebid.js/src/utils.js

    Lines 657 to 668 in 3c70ac7

    export function getValueString(param, val, defaultValue) {
    if (val === undefined || val === null) {
    return defaultValue;
    }
    if (isStr(val)) {
    return val;
    }
    if (isNumber(val)) {
    return val.toString();
    }
    internal.logWarn('Unsuported type for param: ' + param + ' required type: String');
    }

    is only needed by a few appnexus-like bidders.
  • refactor usage of "shim" utility functions that are now part of vanilla javascript. For example:

    Prebid.js/src/utils.js

    Lines 68 to 76 in 3c70ac7

    export let bind = function(a, b) { return b; }.bind(null, 1, uniqueRef)() === uniqueRef
    ? Function.prototype.bind
    : function(bind) {
    var self = this;
    var args = Array.prototype.slice.call(arguments, 1);
    return function() {
    return self.apply(bind, args.concat(Array.prototype.slice.call(arguments)));
    };
    };

    Prebid.js/src/utils.js

    Lines 470 to 479 in 3c70ac7

    export function _map(object, callback) {
    if (isEmpty(object)) return [];
    if (isFn(object.map)) return object.map(callback);
    var output = [];
    _each(object, function (value, key) {
    output.push(callback(value, key, object));
    });
    return output;
    }

    and many others.
@pm-harshad-mane
Copy link
Contributor

Isn't dead code elimination taken care of at the build generation time?

@dgirardi
Copy link
Collaborator Author

dgirardi commented Sep 5, 2023

@pm-harshad-mane, that is correct, but this issue is not about dead code. To the build logic, dead code is code that can be statically determined to be unreachable. If some code in core is reachable only by a module you don't need, it's still kept in the output - unless you run a custom build where that module is not compiled at all.

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

Successfully merging a pull request may close this issue.

3 participants