StreamWriter in editor is very slow. How can I make it instantaneous?

Hi,
I am not able to make the synchronous implementation of StreamWriter write to the hard drive (while in Editor mode and not in play mode) fast. What’s more, the code after the .Close() method is executing without the file having been written to and saved. Can someone please share a code snippet of code of StreamWriter(or any other method) that will make writing text to file instantaneous. Thank you in advance!
This is my code :

using (StreamWriter writer = new StreamWriter(path, false)) { string JSONStringifiedString = JsonConvert.SerializeObject(m_LevelData); char[] JSONStringCharArray = JSONStringifiedString.ToCharArray(); for (int i = 0; i < JSONStringCharArray.Length; i++) { writer.Write(JSONStringCharArray*);* *}* *writer.Flush();* *writer.Close();* *m_WhetherToShowFileSavedLabel = true;* *}*

It is unlikely that the issue you are experiencing is due to the implementation of the StreamWriter itself. It is more likely that there is some other factor causing the delay in writing to the file.

One possible issue could be that the file you are attempting to write to is being used by another process, which could cause a delay in writing to the file. Another possibility is that the file is located on a network drive, which can also cause delays in writing.

One thing you could try is using the FileStream class instead of the StreamWriter class. The FileStream class provides more low-level control over the file, which may allow you to optimize the writing process. Here is an example of how you could use the FileStream class to write the same data as your current code:

using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
    byte[] JSONStringBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(m_LevelData));
    stream.Write(JSONStringBytes, 0, JSONStringBytes.Length);
}

Alternatively, you could try using the File.WriteAllText method, which allows you to write a string to a file in a single call. This may be more efficient than using a loop to write each character individually:

File.WriteAllText(path, JsonConvert.SerializeObject(m_LevelData));