Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active April 23, 2025 12:00
Show Gist options
  • Save Kcko/3f1a530c7c71dd0f9344e9da270a5d2c to your computer and use it in GitHub Desktop.
Save Kcko/3f1a530c7c71dd0f9344e9da270a5d2c to your computer and use it in GitHub Desktop.
// https://medium.com/@asierr/javascripts-function-prototype-tostring-just-got-useful-here-s-how-bc617fd7222c
function greet(name) {
return `Hello, ${name}`;
}
console.log(greet.toString());
// result
"function greet(name) {\n return `Hello, ${name}`;\n}"
// any usage
const fn = x => x * 2;
const body = fn.toString();
if (body.includes('x * 2')) {
console.log('Safe to optimize!');
}
/*
Limitations
Doesn’t work on native functions (e.g., Math.max.toString() returns "function max() { [native code] }")
Some minifiers may still mangle formatting
Not recommended for secure comparisons (function source can be identical but semantically different)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment