From cdca9d52bfe02d74eae0a62edb04839888edd193 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Fri, 10 Mar 2017 20:24:21 -0800 Subject: [PATCH] convert loop to map lookup --- text.go | 29 ++++++++++++++--------------- web_test.go | 4 ++-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/text.go b/text.go index 0667660..13326ef 100644 --- a/text.go +++ b/text.go @@ -51,24 +51,23 @@ func expand(c *gabs.Container, token *list.Element, res *bytes.Buffer) { } res.WriteString("/") children, _ := c.ChildrenMap() - for key, child := range children { - if key == token.Value { - p, skip, err := getPrefix(child) - if err != nil { - fmt.Println(err.Error()) + child, ok := children[token.Value.(string)] + if ok { + p, skip, err := getPrefix(child) + if err != nil { + fmt.Println(err.Error()) + return + } + res.WriteString(p) + if skip { + token = token.Next() + if token == nil { return } - res.WriteString(p) - if skip { - token = token.Next() - if token == nil { - return - } - res.WriteString(token.Value.(string)) - } - expand(child, token.Next(), res) - return + res.WriteString(token.Value.(string)) } + expand(child, token.Next(), res) + return } // handle base case if no keys matched res.WriteString(token.Value.(string)) diff --git a/web_test.go b/web_test.go index 631ec43..8c97d3c 100644 --- a/web_test.go +++ b/web_test.go @@ -133,8 +133,8 @@ func BenchmarkIndexHandler(b *testing.B) { context := &context{config: c} appHandler := &ctxWrapper{context, IndexHandler} handler := http.Handler(appHandler) - req, _ := http.NewRequest("GET", "/p", nil) - req.Host = "sd" + req, _ := http.NewRequest("GET", "/z", nil) + req.Host = "g" rr := httptest.NewRecorder() for n := 0; n < b.N; n++ { handler.ServeHTTP(rr, req)