### Godot version

Godot 4.1-beta2

### System information

Manjaro Gnome, Intel Core i7-11800H

### Issue description

On a relatively modern laptop (Intel Core i7-11800H), in a scene with many scripted nodes (total of 900+ with roughly half of them scripted), adding or duplicating a node freezes the editor for several seconds. Godot 4.0.2 handles such scenes much better!

Here is a flame graph captured while repeatedly duplicating simple Marker2D nodes with Ctrl+D in a complicated scene:
![Screenshot from 2023-06-15 23-54-38](https://github.com/godotengine/godot/assets/2766569/d0019f17-9b82-4a26-9b47-71fa7a3bef3a)

Performance data file to use with hotspot v1.4.1 (relevant data starts at the 75-second mark and lasts about 20+ seconds):
[perf.data.perfparser.zip](https://github.com/godotengine/godot/files/11763700/perf.data.perfparser.zip)



### Steps to reproduce

1) Add 1000 nodes with a script attached.
2) Duplicate a node and witness lags.

Also affects scrolling the scene tree dock.

### Minimal reproduction project

[add_node_lags_repro.zip](https://github.com/godotengine/godot/files/11763731/add_node_lags_repro.zip)

}
			if (!fail) {
				Object *obj = ObjectDB::get_instance(history[i].path[j].object);
				if (obj) {
					Node *n = Object::cast_to<Node>(obj);
					if (n && n->is_inside_tree()) {
						
						continue;
					}
					if (!n) {
						
						continue;
					}
				} 
				fail = true;
			}
			if (fail) {
				break;
			}
		}
		if (fail) {
			history.remove_at(i);
			i--;
		}
	}
	if (current_elem_idx >= history.size()) {
		current_elem_idx = history.size() - 1;
	}
}
void EditorSelectionHistory::add_object(ObjectID p_object, const String &p_property, bool p_inspector_only) {