CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
speedometer.js56 linesDownload Raw Back to helpers
1'use strict';2 3/**4 * Calculate data maxRate5 * @param {Number} [samplesCount= 10]6 * @param {Number} [min= 1000]7 * @returns {Function}8 */9function speedometer(samplesCount, min) {10  samplesCount = samplesCount || 10;11  const bytes = new Array(samplesCount);12  const timestamps = new Array(samplesCount);13  let head = 0;14  let tail = 0;15  let firstSampleTS;16 17  min = min !== undefined ? min : 1000;18 19  return function push(chunkLength) {20    const now = Date.now();21 22    const startedAt = timestamps[tail];23 24    if (!firstSampleTS) {25      firstSampleTS = now;26    }27 28    bytes[head] = chunkLength;29    timestamps[head] = now;30 31    let i = tail;32    let bytesCount = 0;33 34    while (i !== head) {35      bytesCount += bytes[i++];36      i = i % samplesCount;37    }38 39    head = (head + 1) % samplesCount;40 41    if (head === tail) {42      tail = (tail + 1) % samplesCount;43    }44 45    if (now - firstSampleTS < min) {46      return;47    }48 49    const passed = startedAt && now - startedAt;50 51    return passed ? Math.round(bytesCount * 1000 / passed) : undefined;52  };53}54 55export default speedometer;56