Does elisp calculate values of local variables at declaration or when calling them?
Say i want to find out what headlines in buffer are lvl2, and do stuff to them:
(defun map-org-heads ()
(org-element-map (org-element-parse-buffer 'headline) 'headline 'do-stuff))
Would this:
(defun do-stuff (element)
(let ((lvl (org-element-property :level element)))
(if (eq lvl 2)
(call-func-to-do-stuff))))
Have a better performance than this:
(defun do-stuff (element)
(let ((begin (org-element-property :begin element))
(end (org-element-property :end element))
(lvl (org-element-property :level element))
(title (org-element-property :title element)))
(if (eq lvl 2)
(........... do stuff))))
I have quite a big org-file with lots of headlines, so every bit of performance matters.
(...... do stuff)
and(call-func-to-do-stuff)
. – Drew Nov 29 '18 at 17:36org-element-property
is a two-liner that relies upon eitherget-text-property
orplist-get
depending upon whether ELEMENT is astringp
. Neither of these would consume time with a standardorg-mode
. It is the combing through of largeorg-mode
files and examining that information for multiple headings that causes a noticeable time consumption. I personally like to search backward for headings (that interest me) from the bottom of the file to the top because point is at the beginning of a heading at each stop (assumingre-search-backward
is being used). – lawlist Nov 29 '18 at 20:02