Detect if running in Electron.
npm install --save is-electron
import isElectron from 'is-electron';
console.log(isElectron());
Copyright (c) 2016 Cheton Wu
Licensed under the MIT License.
Detect if running in Electron.
License: MIT License
Detect if running in Electron.
npm install --save is-electron
import isElectron from 'is-electron';
console.log(isElectron());
Copyright (c) 2016 Cheton Wu
Licensed under the MIT License.
In the newest version, I think the index.d.ts
is broken.
When using it with typescript 4.5.2, I get the error
[2] node_modules/is-electron/index.d.ts(1,1): error TS1046: Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier.
Because this module detects the runtime, we can assume that the result will always be the same, no matter at which time the function is called.
I did some tests and I think we should cache the result instead of checking all these objects again and again when isElectron()
is called multiple times.
I wrote some code to demonstrate the performance-benefit:
import isElectron from 'is-electron';
// currently
const time0 = performance.now();
for (let i = 0; i < 1000; i++) {
isElectron();
}
const time1 = performance.now();
console.log('Non-Cached ' + (time1 - time0) + 'ms');
// with caching
let _isElectronCached = null;
const isElectronCached = function() {
if (_isElectronCached === null) _isElectronCached = isElectron();
return _isElectronCached;
}
const time2 = performance.now();
for (let i = 0; i < 1000; i++) {
isElectronCached();
}
const time3 = performance.now();
console.log('Cached ' + (time3 - time2) + 'ms');
The results in the current electron-version are:
Non-Cached 0.2699999999999818ms
Cached 0.12999999999988177ms
When the test is run in a non-electron-runtime, all 3 checks will be run which slows this down some more. Here in google chrome:
Non-Cached 13.114999999999782ms
Cached 0.4899999999997817ms
need use
`
import * as isElectron from 'is-electron';
.....
console.log(isElectron());
`
How can I detect the Electron presence when I run Electron with nodeIntegration: false
option?
#5 fixes an error with the typescript typings and was merged but not published, could it be released please?
Why does this module export a function instead of just a constant isElectron: boolean
? Whether or not we are currently in electron should never change, so I don't see why we'd want to have to invoke isElectron()
potentially a bunch of times instead of just running that code once
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.