Skip to content

Commit 63cddf1

Browse files
committed
CLJS-3141: Improve perf of cljs.source-map.base64/encode
1 parent 331fea6 commit 63cddf1

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/main/clojure/cljs/source_map/base64.clj

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
(def char->int (zipmap chars64 (range 0 64)))
1313
(def int->char (zipmap (range 0 64) chars64))
1414

15-
(defn encode [n]
16-
(let [e (find int->char n)]
17-
(if e
18-
(second e)
19-
(throw (Error. (str "Must be between 0 and 63: " n))))))
15+
(defn encode [^long n]
16+
(case n
17+
0 \A 1 \B 2 \C 3 \D 4 \E 5 \F 6 \G 7 \H 8 \I 9 \J 10 \K 11 \L 12 \M
18+
13 \N 14 \O 15 \P 16 \Q 17 \R 18 \S 19 \T 20 \U 21 \V 22 \W 23 \X 24 \Y 25 \Z
19+
26 \a 27 \b 28 \c 29 \d 30 \e 31 \f 32 \g 33 \h 34 \i 35 \j 36 \k 37 \l 38 \m
20+
39 \n 40 \o 41 \p 42 \q 43 \r 44 \s 45 \t 46 \u 47 \v 48 \w 49 \x 50 \y 51 \z
21+
52 \0 53 \1 54 \2 55 \3 56 \4 57 \5 58 \6 59 \7 60 \8 61 \9 62 \+ 63 \/
22+
(throw (Error. (str "Must be between 0 and 63: " n)))))
2023

2124
(defn ^Character decode [c]
2225
(let [e (find char->int c)]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
;; Copyright (c) Rich Hickey. All rights reserved.
2+
;; The use and distribution terms for this software are covered by the
3+
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
;; which can be found in the file epl-v10.html at the root of this distribution.
5+
;; By using this software in any fashion, you are agreeing to be bound by
6+
;; the terms of this license.
7+
;; You must not remove this notice, or any other, from this software.
8+
9+
(ns cljs.source-map.base64-tests
10+
(:require
11+
[clojure.test :refer [deftest is]]
12+
[cljs.source-map.base64 :as base64]))
13+
14+
(deftest encode-test
15+
(doseq [n (range 64)]
16+
(is (= (get base64/int->char n) (base64/encode n))))
17+
(is (thrown-with-msg? Error #"Must be between 0 and 63: 64" (base64/encode 64))))

0 commit comments

Comments
 (0)