Open lots of tabs, e.g. 2500.
```js
var t0 = performance.now();
var tabs = gBrowser.addMultipleTabs(true, 0, new Array(2500).fill({}));
var t1 = performance.now();
gBrowser.removeTabs(tabs);
var t2 = performance.now();
console.log("Total:", t2 - t0, ". Add:", t1 - t0, ". Remove:", t2 - t1);
```
Note `gBrowser.addMultipleTabs` is the API used to restore the previous session, so it's important to optimize it so that Firefox can launch fast.
But it's slow, and one of the reasons is that, when opening each one of these tabs, telemetry runs this: https://searchfox.org/mozilla-central/rev/5bcbe6ae54ee5b152f6cef29dc5627ec7c0e1f1e/browser/modules/BrowserUsageTelemetry.jsm#523
```js
function getOpenTabsAndWinsCounts() {
let loadedTabCount = 0;
let tabCount = 0;
let winCount = 0;
for (let win of Services.wm.getEnumerator("navigator:browser")) {
winCount++;
tabCount += win.gBrowser.tabs.length;
for (const tab of win.gBrowser.tabs) {
if (tab.getAttribute("pending") !== "true") {
loadedTabCount += 1;
}
}
}
return { loadedTabCount, tabCount, winCount };
}
```
Note that the tab cache is invalidated when a tab, so `win.gBrowser.tabs` needs to recompute it for every tab, and then `for (const tab of win.gBrowser.tabs)` makes it quadratic. Is `loadedTabCount` really necessary? Or maybe the number of pending tabs can be cached?
And it seems to me that this doesn't need to happen synchronously for each new tab, the code could be delayed one tick so that it only runs once at the end.
Bug 1810739 Comment 0 Edit History
Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.
Open lots of tabs, e.g. 2500.
```js
var t0 = performance.now();
var tabs = gBrowser.addMultipleTabs(true, 0, new Array(2500).fill({}));
var t1 = performance.now();
gBrowser.removeTabs(tabs);
var t2 = performance.now();
console.log("Total:", t2 - t0, ". Add:", t1 - t0, ". Remove:", t2 - t1);
```
Note `gBrowser.addMultipleTabs` is the API used to restore the previous session, so it's important to optimize it so that Firefox can launch fast.
But it's slow, and one of the reasons is that, when opening each one of these tabs, telemetry runs this: https://searchfox.org/mozilla-central/rev/5bcbe6ae54ee5b152f6cef29dc5627ec7c0e1f1e/browser/modules/BrowserUsageTelemetry.jsm#523
```js
function getOpenTabsAndWinsCounts() {
let loadedTabCount = 0;
let tabCount = 0;
let winCount = 0;
for (let win of Services.wm.getEnumerator("navigator:browser")) {
winCount++;
tabCount += win.gBrowser.tabs.length;
for (const tab of win.gBrowser.tabs) {
if (tab.getAttribute("pending") !== "true") {
loadedTabCount += 1;
}
}
}
return { loadedTabCount, tabCount, winCount };
}
```
Note that the tab cache is invalidated when a tab opens, so `win.gBrowser.tabs` needs to recompute it for every tab, and then `for (const tab of win.gBrowser.tabs)` makes it quadratic. Is `loadedTabCount` really necessary? Or maybe the number of pending tabs can be cached?
And it seems to me that this doesn't need to happen synchronously for each new tab, the code could be delayed one tick so that it only runs once at the end.