mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Mongoose slows down performance of VSCode (TSServer) by a lot

Open ShadiestGoat opened this issue 5 years ago • 217 comments

Do you want to request a feature or report a bug?

bug

What is the current behavior?

It slows down vscode by a lot, even with small projects. I have a very small project, with like 2 small schemas, and the 2 other files, containing on average 160 lines of code, and vscode slows down a lot as soon as I install mongoose. This does not happen when I install most other libraries (haven't tested them all lmao). If I uninstall mongoose, it comes back and works perfectly normal. To clarify "slowing down" means that typescript language server needs 3-5 seconds to realise that I'm importing somethign invalid, and just vscode in general to realise I'm importing something that's never used takes around 2-4 seconds. Usually it happens in under a second so like.. you know... And it's not like vscode is taking a lot of ram/cpu either, it just grows slow. Btw I think this is a mongoose issue, since it happens with no other library, its a special case here Reloading typescript server or project doesn't help

If the current behavior is a bug, please provide the steps to reproduce.

Have vscode to edit your code Install mongoose on your current project (npm i mongoose

My tsconfig.json:

{
    "compilerOptions": {
        "allowJs": true,
        "moduleResolution": "Node",
        "esModuleInterop": true,
        "target": "ES6",
        "resolveJsonModule": true,
        "outDir": "./dist",
        "checkJs": true,
        "module": "CommonJS",
        "lib": ["ESNext"]
    },
    "include": ["src"]
}

What is the expected behavior?

It to not be like this, lmao.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node: v16.2.0 Mongoose: 5.12.13 Linux: 5.12.7-arch1-1

ShadiestGoat avatar Jun 12 '21 22:06 ShadiestGoat

Interesting, can you please provide an example project that demonstrates this? I haven't noticed this but I might be missing something.

vkarpov15 avatar Jun 14 '21 18:06 vkarpov15

Mm well I can replicate it with very little:

import { model, Model, Schema } from "mongoose";

export interface UserSH {
    username: string,
    password: string,
    maxMb: number,
    submitted: string[],
    id: string
}

export const UserSchema = new Schema<UserSH, Model<UserSH>, UserSH>({
    id: "string",
    maxMb: "number",
    username: "string",
    submitted: ["string"],
    password: "string"
})


uiyasdjklsadias //error this baby is undefined

The thing that 'triggers' it is creating the schema. If I just remove the schema creation, its all good

(the only files in the project are the default ones, mongoose, [email protected], and src/index.ts, contents of which are above. Otherwise its newly created)

ShadiestGoat avatar Jun 14 '21 20:06 ShadiestGoat

I can confirm that I am also experiencing a degradation in Typescript performance due to Mongoose.

mjfwebb avatar Jul 03 '21 14:07 mjfwebb

me too

jasonwwl avatar Jul 05 '21 06:07 jasonwwl

I can confirm that too.

I was using @types/mongoose before and just removed it in favor of mongoose own type definitions. It was like snapping two fingers, boom, vscode was now super slow. Simple autocompletes and validations are taking now over 5 or even 10 seconds sometimes in all project files.

cainaf avatar Jul 06 '21 14:07 cainaf

我也能够明显感觉到,mongoose 使得 vscode 加载类型提示变慢。但是仅限于加载 mongoose 相关的内容变慢。

xiaoxin-sky avatar Jul 09 '21 17:07 xiaoxin-sky

Really? For me it slows all content, not just mongoose related...

ShadiestGoat avatar Jul 11 '21 22:07 ShadiestGoat

Same as @ShadiestGoat for me. Even eslint gets very slow. If I delete index.d.ts from mongoose lib inside node_modules, performance of everything becomes instant again.

cainaf avatar Jul 12 '21 14:07 cainaf

I can confirm it slows down all the content for me too.

I did as @cainaf said to verify it was mongoose, I deleted index.d.ts and..., vs code was running fine as hell again. I restored index.d.ts and the performance heavily dropped down again.

fercg16 avatar Jul 13 '21 06:07 fercg16

Well removing new Schema<UserSH, Model<UserSH>, UserSH> removes the issues with performance (on the script I gave above)

ShadiestGoat avatar Jul 13 '21 09:07 ShadiestGoat

I am experiencing the problem since Mongoose has adopted it's own official type definition. I have a workaround that is to delete index.d.ts of mongoose in node_module and then npm i -D -E @types/[email protected] to temporarily use the DefinitelyTyped type definition.

ghost avatar Jul 13 '21 16:07 ghost

Experiencing same problem . Any solutions to this?

TigranAvagyan18 avatar Jul 16 '21 12:07 TigranAvagyan18

And I have the same problem in vscode. I was using postgresql in my project and for some reason I migrated to mongodb (using mongoose). vscode has been super slow since I migrated to mongodb.

qafoori avatar Jul 16 '21 18:07 qafoori

I am experiencing the problem since Mongoose has adopted it's own official type definition. I have a workaround that is to delete index.d.ts of mongoose in node_module and then npm i -D -E @types/[email protected] to temporarily use the DefinitelyTyped type definition.

Did u get any performance change after performing that?

TigranAvagyan18 avatar Jul 18 '21 18:07 TigranAvagyan18

This really needs a fix... Programming is not possible like this.

nikolaninkovninkov avatar Jul 19 '21 13:07 nikolaninkovninkov

So I'm looking into potential reasons for this (just started). To me it just looks like its a long build of the problem, where the types file is just too long/complex. For example, taking an older version of mongoose (eg. 3.11.18), the issue was starting to present itself, given that it decreased performance by a lot, but no where near this level. It could be that to solve this problem the types for this module need a major rework. I also believe this should be a priority.

ShadiestGoat avatar Jul 22 '21 21:07 ShadiestGoat

I'm not sure how much it would help, but perhaps splitting the types into multiple files may help? I'm pretty sure VSCode uses tsserver, which (at least I'm pretty sure of which) has to load an entire file at once

ShadiestGoat avatar Jul 22 '21 22:07 ShadiestGoat

Oh and forgot to mention this earlier, but, I have a pretty good laptop I'm coding on. HP Omen 15, with ryzen 7 4800H, SSD, and a gpu, so it isn't like I just gotta get a better laptop

ShadiestGoat avatar Jul 23 '21 10:07 ShadiestGoat

I created a new TypeScript Node project and it worked fine even just after installing mongoose. However, as soon as I started creating a schema and creating a model from it, things started going south.

This is what the schema + model code looks like.

import * as mongoose from 'mongoose';

export const testSchema = new mongoose.Schema({
    name: String,
    time: Number,
    age: Number
});

export const testModel = mongoose.model('TestModel', testSchema);

I also checked by importing the model and Schema individually instead of using * as but the outcome was the same.

I am a beginner to the world of Mongo and Node so unfortunately I am not knowledgable enough to debug this issue. So instead for the time being, I am going to resort to using the vanilla MongoDB Driver for Node: link.

Sorry mongoose.

----- EDIT -----

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",   
    "module": "commonjs",   
    "strict": true,             
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

package versions

@types/[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Thisura98 avatar Jul 23 '21 15:07 Thisura98

Decided to give it one more shot by taking into consideration feedback by @TigranAvagyan18. And I got it to work!

Commands:

npm i [email protected] --save
npm i @types/[email protected] --save

Summary:

  • Downgrade to last version before Mongoose's own TS types
  • Install DefinitelyTyped's Mongoose types.

My existing code worked with these versions without issues. Code completion performance is also back! It feels so good to have the snappy completion back.

As a side note, Mongoose introduced their own types in version 5.11.0 according to this. 5.10.19 was the last version released before that according to this

I will test some more tonight to see if my current code was affected by this downgrade and update this post if I find any.

Thisura98 avatar Jul 23 '21 16:07 Thisura98