如何测量定义新字符串的 JavaScript 函数的数量

How to measure the number of JavaScript functions that define new Strings

我正在浏览 this paper,其中他们使用 HTMLUnit "measure the number of invocations of JavaScript functions that can be used to define new strings (such as substring, and fromCharCode), and the number of string uses (such as write operations and eval calls)."

我是 HTMLUnit 的新手,我似乎无法弄清楚如何直接查看网站正在使用的功能并计算哪些是 substring、eval 等?具体怎么做?

最简单的方法是对所有本机调用进行 monkeypatch。例如:

const original = String.fromCharCode;
String.fromCharCode = function(...args) {
  console.log('creating new string');
  return original.apply(this, args);
}
console.log(String.fromCharCode(65, 66, 67));

这使您可以保留原始功能,同时仍然可以跟踪正在发生的事情。虽然更复杂,但同样可以应用于(没有双关语意)eval 和任何其他本机调用。
如果你还想监控任何 DOM 变化,你可以使用 MutationObservers.