If you want solid way to do it, you need to run node with --allow-natives-syntax
flag and call this:
%NeverOptimizeFunction(constantTimeStringCompare);
Note that you should call this before you have called constantTimeStringCompare
, if the function is already optimized then this violates an assertion.
Otherwise with
statement is your best bet as making it optimizable would be absolute lunacy whereas supporting try/catch
would be reasonable. You don't need it to affect your code though, this will be sufficient:
function constantTimeStringCompare( a, b ) {
with({});
var valid = true,
length = Math.max( a.length, b.length );
while ( length-- ) {
valid &= a.charCodeAt( length ) === b.charCodeAt( length );
}
// returns true if valid == 1, false if valid == 0
return !!valid;
}
Merely mentioning with
statement corrupts the entire containing function - the optimizations are done at function-level granularity, not per statement.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…