Skip to content

simplify safe_not_equal and not_equal? #2886

Open
@Rich-Harris

Description

@Rich-Harris

I was doing some profiling the other day and was mildly surprised to find that a lot of the time spent in Svelte — possibly even most? — was in safe_not_equal. It occurs to me that we might see some performance gains by changing the implementations thusly:

+function is_mutable(type) {
+	return type === 'object' || type === 'function';
+}
+
export function safe_not_equal(a, b) {
-	return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function');
+	return a !== b || a && is_mutable(typeof a);
}

export function not_equal(a, b) {
-	return a != a ? b == b : a !== b;
+	return a !== b;
}

The downside is that setting x to NaN would trigger an update if x was already NaN. But I'm pretty sure that's a small enough edge case that we needn't worry about it

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerChanges relating to the compiler

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions