Javascript XMLHttpRequest - 获取响应时间

Javascript XMLHttpRequest - obtain response time

如何检索像这样的请求的响应时间..

let xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {

}
};

可以存储请求前的时间,获取请求结束后的时间,将两者相减得到差值:

const before = new Date();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    const now = new Date();
    const diff = now - before;
    console.log("Took " + diff + " milliseconds");
  }
};