Speed up adding a legend

Using ‘webgl’ accelerating don’t help.
Legend adding time is growing.
The more legends I add, the slower they add.
Time (in seconds) with adding legends:

1: drawing time = 0.306192
2: drawing time = 0.6366320000000001
3: drawing time = 0.9132140000000004

Time (in seconds) without adding legends:

1: drawing time = 0.20106599999999997
2: drawing time = 0.3656090000000001
3: drawing time = 0.5057289999999997

Test code.

import time
import numpy as np

from bokeh.layouts import row
from bokeh.plotting import curdoc, figure
from bokeh.models import  ColumnDataSource, Button, Legend, LegendItem

POINTS = 20000
PLOT_WIDTH = 1000
PLOT_HEIGHT = 570

signal_src = {}
legend = Legend(items=[], location="top_right")
signal_figure = figure(plot_height=570, plot_width=1000,
                       tools=["pan,box_zoom,wheel_zoom,save,reset"],
                       x_axis_label='Time', y_axis_label='Amp')
signal_figure.add_layout(legend, 'left')
counter = 0

def add_callback():
    start = time.clock()
    global counter
    counter += 1
    for i in range(5):
        name = f'{counter}_{i}'
        signal_src[name] = ColumnDataSource({'x': np.arange(POINTS),
                                             'y': i * \
                                                  np.sin(2* np.pi * 0.1 * np.arange(POINTS))})
        color = ('blue', 'red', 'black','green', 'yellow', 'orange','pink', 'navy')[i]
        signal_figure.line(x='x', y='y', source=signal_src[name] ,name=name, color=color)
        legend.items.append(LegendItem(label=name,renderers=[signal_figure.renderers[-1]]))
    print(f'{counter}: drawing time = {time.clock() - start}')

add_btn = Button(label="Add", button_type="default")
add_btn.on_click(add_callback)

curdoc().add_root(row(signal_figure, add_btn))

@FreeRP One way to improve this is to collect all the items and then extend the items array once. As it is you are generating separate events (that do work) for every call to append

new_items = []
for i in range(5):
    ...
    new_items.append(LegendItem(...))
legend.items.extend(new_items)