Skip to content

manually inlining validator functions improves performance #154

@Uzlopak

Description

@Uzlopak

Lets take the isAbsolute function of posix in the path module.

  /**
   * @param {string} path
   * @returns {boolean}
   */
  isAbsolute(path) {
    validateString(path, 'path');
    return path.length > 0 &&
           StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH;
  },

Benchmark it:

./node benchmark/path/isAbsolute-posix.js 
path/isAbsolute-posix.js n=100000 path="": 26,199,553.088023424
path/isAbsolute-posix.js n=100000 path=".": 19,512,560.625525866
path/isAbsolute-posix.js n=100000 path="/foo/bar": 15,003,530.33068681
path/isAbsolute-posix.js n=100000 path="/baz/..": 20,841,496.252698973
path/isAbsolute-posix.js n=100000 path="bar/baz": 25,044,717.342815597

inline validateString

  /**
   * @param {string} path
   * @returns {boolean}
   */
  isAbsolute(path) {
    if (typeof path !== 'string')
      throw new ERR_INVALID_ARG_TYPE('path', 'string', path);
    return path.length > 0 &&
           StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH;
  },
./node benchmark/path/isAbsolute-posix.js 
path/isAbsolute-posix.js n=100000 path="": 49,143,043.605605446
path/isAbsolute-posix.js n=100000 path=".": 36,665,695.025748484
path/isAbsolute-posix.js n=100000 path="/foo/bar": 20,950,367.322790273
path/isAbsolute-posix.js n=100000 path="/baz/..": 22,806,405.772027623
path/isAbsolute-posix.js n=100000 path="bar/baz": 41,722,470.470921524

Annoying...

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