Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Performance issues with contextBridge #41511

Closed
3 tasks done
iptop opened this issue Mar 5, 2024 · 3 comments
Closed
3 tasks done

[Bug]: Performance issues with contextBridge #41511

iptop opened this issue Mar 5, 2024 · 3 comments
Labels

Comments

@iptop
Copy link

iptop commented Mar 5, 2024

Preflight Checklist

Electron Version

25.9.8

What operating system are you using?

Windows

Operating System Version

Windows 11 23H2

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

Because function calls through contextBridge actually involve parameter passing by copying, this leads to poor performance in scenarios with large amounts of data.

preload.js

const { contextBridge, ipcRenderer } = require('electron')
let arr = new ArrayBuffer(1080 * 1920 * 4)
function testFunc (cb) {
  cb(arr)
}

contextBridge.exposeInMainWorld('electron', {
  testFunc
})

WEB.js

let s = (new Date()).getTime()
window.electron.testFunc(()=>{
  let e = (new Date()).getTime()
  console.log(e-s) // 10ms
})

Actual Behavior

I hope there is a way, like postMessage, to transfer control and avoid a large amount of data copying.

Testcase Gist URL

No response

Additional Information

No response

@iptop iptop added the bug 🪲 label Mar 5, 2024
@Prinzhorn
Copy link
Contributor

First search result I found #27024

So you can use postMessage between renderer and the preload script.

The main reason to use contextBridge is for synchronous messaging between the isolated world and main world. If you don't need synchronous messaging, window.postMessage works just as well.

#27024 (comment)

@iptop
Copy link
Author

iptop commented Mar 6, 2024

@Prinzhorn Your response successfully solved my problem.However, I have a question. Since it's supposed to be context isolated, why can the preload.js file access the same window object as the rendering process?

@iptop iptop closed this as completed Mar 6, 2024
@Prinzhorn
Copy link
Contributor

Read the linked issue:

#27024 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants