4

When specifying a key sequence you have the option of using a kbd, string, or vector. Which one of these is faster? More efficient? Optimal in particular situations? Any trade-offs? Please list your reasoning and why.

  • 1
    Why do you care about the speed in this case? Do you have some indication that there is a speed difference or that the speed matters here? Other than the speed question, see @phils's answer: clarity for human readers. kbd is all about human readers. – Drew Jan 9 '18 at 15:46
  • Just curious is all. – user9111980 Jan 9 '18 at 20:51
12

I would recommend using kbd in general, because of the way it eliminates any guess work in how to specify a key sequence. That benefit tends to outweighs everything else, IMO.

I actually thought that kbd was going to be 'slower' (by some entirely inconsequential margin) because it's not a macro, but a quick test to validate my assumptions shows that byte-compiled code actually contains the string or vector form, so apparently there is no speed benefit whatsoever to be had in avoiding kbd.

  • Oh wow. I didn't know that. Great answer. – user9111980 Jan 9 '18 at 9:50
  • 4
    kbd is a "pure" function, so the byte compiler can substitute the result if you give a constant argument: (get 'kbd 'pure) => t – npostavs Jan 9 '18 at 13:35
  • 1
    In fact, kbd was originally a Lisp macro (in subr.el). – Drew Jan 9 '18 at 15:47
  • 1
    I think the slowest aspect of using non-byte-compiled calls to kbd nowadays is the loading of its dependency lisp/edmacro.el. – Basil Jan 9 '18 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.