Skip to content

Commit c189af2

Browse files
committed
Add: Use buffer-local-value instead of with-current-buffer
1 parent c45a3ef commit c189af2

File tree

2 files changed

+1196
-1155
lines changed

2 files changed

+1196
-1155
lines changed

README.org

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ This file also defines macros =thunk-let= and =thunk-let*= that are analogous to
123123

124124
*** Best practices :best_practices:
125125

126+
**** Accessing buffer-local variables
127+
128+
It's much faster to use ~buffer-local-value~ than ~with-current-buffer~ to access the value of a variable in a buffer.
129+
130+
#+BEGIN_SRC elisp
131+
(bench-multi :times 1000 :ensure-equal t
132+
:forms (("buffer-local-value" (--filter (equal 'magit-status-mode (buffer-local-value 'major-mode it))
133+
(buffer-list)))
134+
("with-current-buffer" (--filter (equal 'magit-status-mode (with-current-buffer it
135+
major-mode))
136+
(buffer-list)))))
137+
#+END_SRC
138+
139+
#+RESULTS:
140+
| Form | x faster than next | Total runtime | # of GCs | Total GC runtime |
141+
|---------------------+--------------------+---------------+----------+------------------|
142+
| buffer-local-value | 50.34 | 0.047657734 | 0 | 0.0 |
143+
| with-current-buffer | slowest | 2.399323452 | 0 | 0.0 |
144+
126145
**** Inserting strings :strings:
127146

128147
Inserting strings into buffers with ~insert~ is generally fast, but it can slow down in buffers with lots of markers or overlays. In general, it can be faster to insert one large string (which may include newlines). For example:

0 commit comments

Comments
 (0)