From 09864fd277aedfb92bfb2a7a5aeea4a11275cc05 Mon Sep 17 00:00:00 2001
From: Jan Potoms <2109932+Janpot@users.noreply.github.com>
Date: Fri, 13 Aug 2021 10:26:55 +0200
Subject: [PATCH] improve compactIri performance

---
 lib/compact.js | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/lib/compact.js b/lib/compact.js
index 1bcdb4bb..13539ecd 100644
--- a/lib/compact.js
+++ b/lib/compact.js
@@ -929,13 +929,13 @@ api.compactIri = ({
 
   // If iri could be confused with a compact IRI using a term in this context,
   // signal an error
-  for(const [term, td] of activeCtx.mappings) {
-    if(td && td._prefix && iri.startsWith(term + ':')) {
-      throw new JsonLdError(
-        `Absolute IRI "${iri}" confused with prefix "${term}".`,
-        'jsonld.SyntaxError',
-        {code: 'IRI confused with prefix', context: activeCtx});
-    }
+  const scheme = iri.substr(0, iri.indexOf(':'));
+  const td = scheme && activeCtx.mappings.get(scheme);
+  if(td && td._prefix) {
+    throw new JsonLdError(
+      `Absolute IRI "${iri}" confused with prefix "${scheme}".`,
+      'jsonld.SyntaxError',
+      {code: 'IRI confused with prefix', context: activeCtx});
   }
 
   // compact IRI relative to base