allocate memory for document and write with slice lookup