Resuse the gson instance when parsing source maps
Gson object are kind of expensive to construct (due to the thread local instances).
@@ -29,12 +29,17 @@ import java.util.Map;
* Java implementation of the source map parser.
*/
public class SourceMapObjectParser {
+ // Gson objects are safe to share across threads and are somewhat expensive to construct. Use a
+ // single instance. This is Safe per gson docs:
+ // https://google.github.io/gson/apidocs/com/google/gson/Gson.html
+ private static final Gson gson = new Gson();
+
public static SourceMapObject parse(String contents) throws SourceMapParseException {
SourceMapObject.Builder builder = SourceMapObject.builder();
try {
- JsonObject sourceMapRoot = new Gson().fromJson(contents, JsonObject.class);
+ JsonObject sourceMapRoot = gson.fromJson(contents, JsonObject.class);
builder.setVersion(sourceMapRoot.get("version").getAsInt());
builder.setFile(getStringOrNull(sourceMapRoot, "file"));
