Add non-keyword fast path check if first char is '@'.
Avoids full switch for data that can't be a keyword.
Array access will just be undefined for empty string.
Performance change depends on data shape, format, and processing algorithm workload. Some synthetic tests with ~80% non-keywords could see 1-3%+ overall increase.
I tried various permutations of other isKeyword optimizations. The benchmarking code had too much noise to tell what was best. But this simple change does appear to have some effect for some (most?) workloads. I did try a few other things, which should maybe be further explored if needed, but I couldn't tell if they were better or not:
Change switch to keywordSet.has(v).
Change switch to v in kewordsObject.
Change switch to kewordsObject.hasOwnProperty(v).
Moving the probably popular @context, @id, @type to be first in switch or to an earlier fast path switch, explicity if checks, or other constructs.
If someone wants to dig into the optimizer to figure out the best way to run this particular check, that could be interesting. I think better benchmarking code is needed to check otherwise.
switch
for data that can't be a keyword.I tried various permutations of other
isKeyword
optimizations. The benchmarking code had too much noise to tell what was best. But this simple change does appear to have some effect for some (most?) workloads. I did try a few other things, which should maybe be further explored if needed, but I couldn't tell if they were better or not:switch
tokeywordSet.has(v)
.switch
tov in kewordsObject
.switch
tokewordsObject.hasOwnProperty(v)
.@context
,@id
,@type
to be first inswitch
or to an earlier fast pathswitch
, explicityif
checks, or other constructs.If someone wants to dig into the optimizer to figure out the best way to run this particular check, that could be interesting. I think better benchmarking code is needed to check otherwise.