If you need a reliable way to do this, you need to run node with the --allow-natives-syntax flag and call this:
%NeverOptimizeFunction(constantTimeStringCompare);
Note that you must call this before you call constantTimeStringCompare , if the function is already optimized, this violates the statement.
Otherwise, the with statement is your best bet, since optimization will be absolutely insane, while try/catch support will be reasonable. You do not need this to affect your code, but that will be enough:
function constantTimeStringCompare( a, b ) { with({}); var valid = true, length = Math.max( a.length, b.length ); while ( length-- ) { valid &= a.charCodeAt( length ) === b.charCodeAt( length ); }
A simple mention of the with statement distorts the entire function contained - optimization is performed at the level of detail at the function level, and not at each statement.
Esailija Aug 28 '13 at 12:46 on 2013-08-28 12:46
source share