Skip to content

Performance of =, !=, >=, and <= #245

@CIVITAS-John

Description

@CIVITAS-John

@maizi reports that the way we implement the three basic operators could be further optimized.

I did some inspection and at this time, my instinction is that the optimization of operators could be a key part to improve the performance of NLW. Specifically, >= and <= should have their own implementations. It won't be hard:

lte: (a, b) ->
  if (checks.isString(a) and checks.isString(b)) or (checks.isNumber(a) and checks.isNumber(b))
    a <= b
  else if typeof(a) is typeof(b) and a.compare? and b.compare?
    result = a.compare(b) 
    result is LT or result is EQ
  else
    throw exceptions.internal("Invalid operands to `lte`")

For = and !=, my intuition is that the null check is not necessary: if someone send in a null or undefined, just let it be. Since this would only happen when a primitive or an extension primitive accidentally leaks them, it is unnecessary to put those checks in place.

Those are not enough - see the following benchmark:
let i 0 let l length arr while[i < l][... set i i + 1]
performs much worse than foreach either with or without calculating the index.

However, my proposal is relatively safer to integrate without compromising the symantic integrity of NLW. If we could sacrifice this as well - probably with a compiler option - then we could do even more: override the valueOf and convert everything back to Javascript operators.

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