buffer: let Buffer.of use heap #60503
                
     Open
            
            
          
      
        
          +3
        
        
          −2
        
        
          
        
      
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
See also #60372.
This is a somewhat less performant version than that PR, but significantly faster than
mainand is non semver-major (so it can be backported) and does not need doc changes.Buffer.ofis usually used on small static sizes.The assumption that
createUnsafeBuffer(size)is faster thannew Uint8Array(size)/new FastBuffer(size)is incorrect for small sizes, as the latter does not call our allocator at all when it can go to heap, and the former is always an alloc.Not letting it go to heap is hurting performance, direct allocations are slow.
See #26301 for context
But even past the
v8_typed_array_max_size_in_heapsize (which is 64 bytes in the default config), this is still not worse than the old version way past expectedBuffer.ofsizesBefore:
After:
For comparison, #60372 (even faster, but does that via pooling):
Realistically, sizes 0-8 are most important.
Benchmark code (taken from #60372 with some adjustments):