aboutsummaryrefslogtreecommitdiff
path: root/src/js/node/os.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/node/os.js')
-rw-r--r--src/js/node/os.js64
1 files changed, 63 insertions, 1 deletions
diff --git a/src/js/node/os.js b/src/js/node/os.js
index 2ff98beea..40250ef9a 100644
--- a/src/js/node/os.js
+++ b/src/js/node/os.js
@@ -14,10 +14,72 @@ export var tmpdir = function () {
return tmpdir();
};
+// os.cpus() is super expensive
+// Specifically: getting the CPU speed on Linux is very expensive
+// Some packages like FastGlob only bother to read the length of the array
+// so instead of actually populating the entire object
+// we turn them into getters
+function lazyCpus({ cpus }) {
+ return () => {
+ const array = new Array(navigator.hardwareConcurrency);
+ function populate() {
+ const results = cpus();
+ const length = results.length;
+ array.length = length;
+ for (let i = 0; i < length; i++) {
+ array[i] = results[i];
+ }
+ }
+
+ for (let i = 0; i < array.length; i++) {
+ // This is technically still observable via
+ // Object.getOwnPropertyDescriptors(), but it should be okay.
+ const instance = {
+ get model() {
+ if (array[i] === instance) populate();
+ return array[i].model;
+ },
+ set model(value) {
+ if (array[i] === instance) populate();
+ array[i].model = value;
+ },
+
+ get speed() {
+ if (array[i] === instance) populate();
+ return array[i].speed;
+ },
+
+ set speed(value) {
+ if (array[i] === instance) populate();
+ array[i].speed = value;
+ },
+
+ get times() {
+ if (array[i] === instance) populate();
+ return array[i].times;
+ },
+ set times(value) {
+ if (array[i] === instance) populate();
+ array[i].times = value;
+ },
+
+ toJSON() {
+ if (array[i] === instance) populate();
+ return array[i];
+ },
+ };
+
+ array[i] = instance;
+ }
+
+ return array;
+ };
+}
+
function bound(obj) {
return {
arch: obj.arch.bind(obj),
- cpus: obj.cpus.bind(obj),
+ cpus: lazyCpus(obj),
endianness: obj.endianness.bind(obj),
freemem: obj.freemem.bind(obj),
getPriority: obj.getPriority.bind(obj),